Javascript remove object from array by property

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?

asked Mar 8, 2013 at 6:14

Javascript remove object from array by property

imperium2335imperium2335

22.4k38 gold badges107 silver badges186 bronze badges

0

One possibility:

myArray = myArray.filter(function( obj ) {
    return obj.field !== 'money';
});

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 variable myArray with the new reference. Use with caution.

Javascript remove object from array by property

Charlie

21.6k10 gold badges55 silver badges86 bronze badges

answered Mar 8, 2013 at 6:19

Javascript remove object from array by property

8

Iterate through the array, and splice out the ones you don't want. For easier use, iterate backwards so you don't have to take into account the live nature of the array:

for (var i = myArray.length - 1; i >= 0; --i) {
    if (myArray[i].field == "money") {
        myArray.splice(i,1);
    }
}

Javascript remove object from array by property

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.

myArray.splice(myArray.findIndex(item => item.field === "cStatus"), 1)

answered Apr 6, 2018 at 9:45

PeracekPeracek

1,61114 silver badges17 bronze badges

3

In ES6, just one line.

const arr = arr.filter(item => item.key !== "some value");

:)

answered Jan 21, 2020 at 3:27

Javascript remove object from array by property

1

You can use lodash's findIndex to get the index of the specific element and then splice using it.

myArray.splice(_.findIndex(myArray, function(item) {
    return item.value === 'money';
}), 1);

Update

You can also use ES6's findIndex()

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.

const itemToRemoveIndex = myArray.findIndex(function(item) {
  return item.field === 'money';
});

// proceed to remove an item only if it exists.
if(itemToRemoveIndex !== -1){
  myArray.splice(itemToRemoveIndex, 1);
}

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.

  1. Using filter method
testArray.filter(prop => prop.key !== 'Test Value')
  1. Using splice method. For this method we need to find the index of the propery.
const index = testArray.findIndex(prop => prop.key === 'Test Value')
testArray.splice(index,1)

answered Nov 23, 2020 at 17:52

Javascript remove object from array by property

SenthuranSenthuran

1,2991 gold badge14 silver badges18 bronze badges

Here's another option using jQuery grep. Pass true as the third parameter to ensure grep removes items that match your function.

users = $.grep(users, function(el, idx) {return el.field == "money"}, true)

If you're already using jQuery then no shim is required, which is could be useful as opposed to using Array.filter.

answered Mar 23, 2015 at 12:20

Javascript remove object from array by property

sifridaysifriday

4,2921 gold badge12 silver badges24 bronze badges

var myArray = [
    {field: 'id', operator: 'eq', value: id}, 
    {field: 'cStatus', operator: 'eq', value: cStatus}, 
    {field: 'money', operator: 'eq', value: money}
];
console.log(myArray.length); //3
myArray = $.grep(myArray, function(element, index){return element.field == "money"}, true);
console.log(myArray.length); //2

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.

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

 var items = [ 
  { "id": 3.1, "name": "test 3.1"}, 
  { "id": 22, "name": "test 3.1" }, 
  { "id": 23, "name": "changed test 23" } 
  ]

    function removeByKey(array, params){
      array.some(function(item, index) {
        return (array[index][params.key] === params.value) ? !!(array.splice(index, 1)) : false;
      });
      return array;
    }

    var removed = removeByKey(items, {
      key: 'id',
      value: 23
    });

    console.log(removed);

answered Jul 19, 2018 at 20:36

Javascript remove object from array by property

JackTheKnifeJackTheKnife

3,3626 gold badges50 silver badges99 bronze badges

Using the lodash library:

var myArray = [
    {field: 'id', operator: 'eq', value: 'id'}, 
    {field: 'cStatus', operator: 'eq', value: 'cStatus'}, 
    {field: 'money', operator: 'eq', value: 'money'}
];
var newArray = _.remove(myArray, function(n) {
  return n.value === 'money';;
});
console.log('Array');
console.log(myArray);
console.log('New Array');
console.log(newArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>

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:

var myArray = [
    {field: 'id', operator: 'eq', value: 'id'}, 
    {field: 'cStatus', operator: 'eq', value: 'cStatus'}, 
    {field: 'money', operator: 'eq', value: "money"}
];

myArray.remove_key = function(key){
    var i = 0, 
        keyval = null;
    for( ; i < this.length; i++){
        if(this[i].field == key){
            keyval = this.splice(i, 1);
            break;
        }
    }
    return keyval;
}

answered Mar 8, 2013 at 6:25

Javascript remove object from array by property

Rob M.Rob M.

34k6 gold badges50 silver badges46 bronze badges

2

Following is the code if you are not using jQuery. Demo

var myArray = [
    {field: 'id', operator: 'eq', value: 'id'}, 
    {field: 'cStatus', operator: 'eq', value: 'cStatus'}, 
    {field: 'money', operator: 'eq', value: 'money'}
];

alert(myArray.length);

for(var i=0 ; i<myArray.length; i++)
{
    if(myArray[i].value=='money')
        myArray.splice(i);
}

alert(myArray.length);

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

Javascript remove object from array by property

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.