Filtering array using $ .grep failed
2 answers
$.grep()
returns a new filtered array and does not affect the original array.
Try:
var filteredArray = $.grep(Arr.nameList, function(e){
return e.id != 1;
});
console.log(filteredArray);
What you were doing was creating a new filtered array but ignoring it and checking the original unaffected array
Link: $. grep () docs
0
source to share
jQuery.grep Description: Finds the elements of an array that satisfy a filter function. The original array is unaffected.
- First you need to store the returned array in a variable.
- Remove
.nameList
you don't need to add anything, just your arrayArr
.
Replace:
$.grep(Arr.nameList , function(e){
return e.id != 1;
});
By:
var result = $.grep(Arr , function(e){
return e.id != 1;
});
You can find the grep result:
console.log(result);
Take a look at Working Violin .
+1
source to share