如何从数组中获取与特定值匹配的多个元素

时间:2013-02-27 19:37:52

标签: javascript

我想知道如何从数组中获取与数值匹配的特定元素。以下是我正在努力实现这一目标的练习的一部分:

function countCoins() {
    var Coins = document.clubForm.coinJar.value;

    if(Coins != '' && isNaN(Coins) != true) {
        var coinType = Coins.split(",");

        for(Compare = 0; Compare < coinType.length; Compare++) {
            if(Compare == '1') {
                var PennyCount = document.getElementById('Penny');
                PennyCount.innerHTML = pennies.length;
            }
        }

因此,当我对coinType数组进行迭代时,如何将多个等于1的值放入新数组中?

1 个答案:

答案 0 :(得分:0)

 var _arrHolder = [];
 function countCoins()
 {
    var Coins = document.clubForm.coinJar.value;

    if(Coins != '' && isNaN(Coins) != true)
    {
        var coinType = Coins.split(",");

    for ( Compare = 0; Compare < coinType.length; Compare++)
    {
    if(Compare == '1')
        {
        var PennyCount = document.getElementById('Penny');
        PennyCount.innerHTML = pennies.length;
        _arrHolder.push(coinType[Compare]); // <-- push what you want into the array here.
        }
   }