I am trying to remove an array in a multidimensional array if the id is the same off the given one.
var test_arr = [{"name":"qqq", "city":"it","id":"123456"}, {"name":"ggg", "city":"uk","id":"777456"}];
var result = test_arr.filter(function(v,i) {
if (v[0] === '123456'){ test_arr.splice(i,1) }
});
//alert( test_arr[0].id)
alert(result)
http://jsfiddle.net/ofLg0vq5/2/
How Could I do this?
The issue with your current solution is that you're not using
.filtercorrectly..filterexpects its passed function to return aboolean. Iftrueis returned, the current element will be kept in the newly generated array. If it isfalse, the current element will be omitted. So, instead of trying to remove the element fromtest_arrusing.splice, use.filterto decide what stays and what gets removed.Also, note that in your example
vis referring to a given element (a particular object) in yourtest_array. Thus, you do not need to target index0of your object, but rather you need to get theidof the current object.If you want a "cleaner" solution you can use an arrow function with destructing assignment: