0

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

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 的值放入一个新数组中?

4

1 回答 1

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.
        }
   }
于 2013-02-27T19:40:37.150 回答