繁体   English   中英

在for循环中填充数组

[英]Fill array in a for loop

我正在尝试在循环中填充新的数组,但无法弄清楚。

我需要的是,我将从临时数组中筛选出所有值以设置为新数组。 所有以“ +”开头的值都需要进入数组“ my_hash_plus”。 这就是我现在所拥有的。

var my_hash_plus = new Array;   
$.each(my_hash_plus_tmp, function(_, temp) {
    if (tmp[0] == "+") {
        my_hash_plus[] = temp;
    }
});

我注意到我无法在js中用[]填充数组,但是如果我my_hash_plus了这些情况,那么my_hash_plus就会变成普通的var ...所以我的问题是:从数组my_hash_plus_tmp中获取所有值的最佳方法是以“ +”开头进入一个新数组(我将其称为my_hash_plus

使用push()

var my_hash_plus = [];   

$.each(my_hash_plus_tmp, function(_, temp) {
    if (temp.charAt(0) == "+") {
        my_hash_plus.push( temp );
    }
});

在更新的浏览器中,即使没有jQuery,也可以轻松完成

var my_hash_plus = my_hash_plus_tmp.filter(function(it){return it.charAt(0)=='+'});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM