Basically, I want to scan some array which contain a key word inside, for example:
arr = [ {"name":"apple", "count": 2},
{"name":"orange", "count": 5},
{"name":"pear", "count": 3},
{"name":"orange 356", "count": 16},
{"name":"orange fsa", "count": 15},]
I would like to get orange, orange 356 and orange fsa. I have tried to use different method of Jquery to get my function done.
var newArr = arr.filter(function(item){
return item.name == "orange"; // tried = and === but no luck
});
console.log("Filter results:",newArr);
and alternative:
var newArr = [];
for(var i= 0, l = arr.length; i< l; i++){
if(arr[i].name = "orange" ){
newArr.push(arr[i]);
}
}
console.log("Filter results:",newArr);
But it only returns the arr[1] for me, how may I able to get everything contains "orange"?
Solution:
var newArr = [];
var newArr = arr.filter(function(item){
return item.eman.indexOf("orange")!=-1;
});
document.getElementById("123").innerHTML = "Filter results:"+JSON.stringify(newArr);