我有一个JavaScript数组:
var j_array = new Array();
j_arry=["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10"];
我需要找到类要来的次数和它的数组键,所以我使用:
found = $.inArray('class', j_array); ` But it returns `-1`;
然后我用:
var search = 'class';
$.each([j_array], function(index, value){
$.each(value, function(key, cell){
if (search.indexOf(cell) !== -1)
console.log('found in array '+index, cell);
});
});
但那也是错误的。我怎么解决这个?
我想从这个数组中得到以下内容:
>
类来了4次,在键0、2、3和7处
我只想创建一个单独的class数组,
new_array = ["class:1", "class:2", "class:3", "class:10"];
当前,j_array
中有四个类。如何获取第n个类值
即第一个类值=“class:1”
,第二个类值=“class:5”
等。
使用array.prototype.filter
筛选出包含string类的数组的元素-请参阅下面的演示:
null
var j_array =["class:1","division:a","class:5","class:3","division:b","division:c","division:d","class:10"];
var result = j_array.filter(function(e){
return e.indexOf('class')!==-1;
});
console.log(result);