I want to remove specific elements in the original array (which is var a
). I filter()
that array and splice()
returned new array. but
You can either make changes to the original array:
var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
a = a.filter(function (e) {
return e.name === 'tc_001';
});
a.splice(0,1);
Or in your own code just apply to a
everything you've done to b
a = b
[edit]
Perhaps if I expand on what each action does, it will be more clear.
var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
// You now have array a with 3 elements.
b = a.filter(function (e) {
return e.name === 'tc_001';
});
// a is still 3 elements, b has one element (tc_001)
b.splice(0,1);
// You remove the first element from b, so b is now empty.
If you want to remove the elements tc_002
and tc_003
from a
but keep them in b
, this is what you can do:
var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
b = a.filter(function (e) {
return e.name != 'tc_001';
});
a = a.filter(function(item) { return b.indexOf(item) == -1; });
console.log(a); // tc_001
console.log(b); // tc_002, tc_003