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
.filter
correctly..filter
expects its passed function to return aboolean
. Iftrue
is 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_arr
using.splice
, use.filter
to decide what stays and what gets removed.Also, note that in your example
v
is referring to a given element (a particular object) in yourtest_array
. Thus, you do not need to target index0
of your object, but rather you need to get theid
of the current object.If you want a "cleaner" solution you can use an arrow function with destructing assignment: