I have an array of objects like so: Show
How do I remove a specific one based on its property? e.g. How would I remove the array object with 'money' as the field property? asked Mar 8, 2013 at 6:14
imperium2335imperium2335 22.4k38 gold badges107 silver badges186 bronze badges 0 One possibility:
Please note that
Charlie 21.6k10 gold badges55 silver badges86 bronze badges answered Mar 8, 2013 at 6:19
8 Iterate through the array,
and
user1438038 5,6776 gold badges57 silver badges85 bronze badges answered Mar 8, 2013 at 6:17
9 Say you want to remove the second object by it's field property. With ES6 it's as easy as this.
answered Apr 6, 2018 at 9:45
PeracekPeracek 1,61114 silver badges17 bronze badges 3 In ES6, just one line.
:) answered Jan 21, 2020 at 3:27
1 You can use lodash's findIndex to get the index of the specific element and then splice using it.
Update You can also use ES6's findIndex()
answered Aug 20, 2015 at 11:30
SridharSridhar 10.9k5 gold badges38 silver badges41 bronze badges 6 We can remove the element based on the property using the below 2 approaches.
answered Nov 23, 2020 at 17:52
SenthuranSenthuran 1,2991 gold badge14 silver badges18 bronze badges Here's another option using jQuery grep. Pass
If you're already using jQuery then no shim is required, which is could be
useful as opposed to using answered Mar 23, 2015 at 12:20
sifridaysifriday 4,2921 gold badge12 silver badges24 bronze badges
Element is an object in the array. 3rd parameter answered Aug 25, 2016 at 17:00
0 Based on some comments above below is the code how to remove an object based on a key name and key value
answered Jul 19, 2018 at 20:36
JackTheKnifeJackTheKnife 3,3626 gold badges50 silver badges99 bronze badges Using the lodash library:
Greg Pettit 10.6k5 gold badges52 silver badges71 bronze badges answered Apr 10, 2018 at 9:41
Parth RavalParth Raval 3,7513 gold badges21 silver badges34 bronze badges jAndy's solution is probably best, but if you can't rely on filter you could do something like:
answered Mar 8, 2013 at 6:25
Rob M.Rob M. 34k6 gold badges50 silver badges46 bronze badges 2 Following is the code if you are not using jQuery. Demo
You can also use underscore library which have lots of function. Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support answered Mar 8, 2013 at 6:27
Umair SaleemUmair Saleem 1,0355 silver badges19 bronze badges 1 How do you remove an object from an array from a property?To remove an object from an array by its value:
Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.
How do I remove an object from an array with JavaScript?There are different methods and techniques you can use to remove elements from JavaScript arrays:. pop - Removes from the End of an Array.. shift - Removes from the beginning of an Array.. splice - removes from a specific Array index.. filter - allows you to programatically remove elements from an Array.. How do I remove an object from an array by id?To remove an element from an array by ID in JavaScript, use the findIndex() method to find the index of the object with the ID in the array. Then call the splice() method on the array, passing this index and 1 as arguments to remove the object from the array.
How do you remove the property name from this object?Remove Property from an Object
The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.
|