Hi I delete an object in an array using two approaches:- splice and filter.
splice code here:-
(this.myArray).splice((this.myArray).indexOf(myobject), 1);
filter code here:-
(this.myArray).filter(obj => obj !== myobject);
Please tell us differences between both and which one is the best approach?
Check this performance test out. It's has not that much to do with Angular, but more with JavaScript. If you're able just go for the fastest method available :)
I think that the main difference here is:
- splice - lets you remove an element from this particular array
- filter - won't touch the input array and will create and return new filttered array
angular has nothing to do here and when it comes to speed, splice will win
and small test as proof https://jsperf.com/array-splice-vs-array-filter/1
In case you know the index using splice would be an O(1) operation while using filter is an O(n) operation.
来源:https://stackoverflow.com/questions/44435141/remove-object-in-array-using-filter-and-splice-which-one-is-best-approach-in-ang