Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing objects from array

Tags:

javascript

i have an array = [] and it contains objects... obj, obj, obj. I have to remove obj two, but i don't know the index... so how can i remove obj. the name of the object is also same.

like image 342
theJava Avatar asked Mar 06 '26 17:03

theJava


1 Answers

The ES5 way to do this is with Array.filter

 myarray = myarray.filter(function(val, index, a) {
    return (val !== obj);
 });

See the MDN page linked above for a version to use if you don't have ES5.

Technically this creates a new array, rather than modify the original array. However splice as proposed in other answers isn't particularly efficient anyway since it has to renumber all of the indices above each matching element, and will do so over and over if there's more than one match.

like image 123
Alnitak Avatar answered Mar 08 '26 07:03

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!