I have an array of objects like so:
var myArray = [
{field: 'id', operator: 'eq', value: id},
{field: 'cStatus', operator: 'eq', value: cStatus},
{field: 'money', operator: 'eq', value: money}
];
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?
Here's another option using jQuery grep. Pass
true
as the third parameter to ensure grep removes items that match your function.If you're already using jQuery then no shim is required, which is could be useful as opposed to using
Array.filter
.One possibility:
Please note that
filter
creates a new array. Any other variables referring to the original array would not get the filtered data although you update your original variablemyArray
with the new reference. Use with caution.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()
Element is an object in the array. 3rd parameter
true
means will return an array of elements which fails your function logic,false
means will return an array of elements which fails your function logic.