45

I have an array of objects:

[{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]

How I can delete this item {"value":"14","label":"7"} resulting in the new array:

 [{"value":"14","label":"7"},{"value":"18","label":"7"}]

?

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
Arthur Yakovlev
  • 8,933
  • 8
  • 32
  • 48

9 Answers9

92

In ES6 (or using es6-shim) you can use Array.prototype.findIndex along with Array.prototype.splice:

arr.splice(arr.findIndex(matchesEl), 1);

function matchesEl(el) {
    return el.value === '14' && el.label === '7';
}

Or if a copy of the array is ok (and available since ES5), Array.prototype.filter's the way to go:

var withoutEl = arr.filter(function (el) { return !matchesEl(el); });
Noah Freitas
  • 17,240
  • 10
  • 50
  • 67
40

Solution with Identity

If you have object identity not just object equality (i.e. you're trying to delete a specific object from the array, not just an object that contains the same data as an existing object) you can do this very simply with splice and indexOf:

a = {x:1}
b = {x:2}
arr = [a,b]

Say you want to remove b:

arr.splice(
  arr.indexOf(b), 1
);

Solution with Equality

The question makes it unclear whether we are dealing with identity or equality. If you have equality (two objects containing the same stuff, which look the same as each other, but are not actually the same object), you'll need a little comparison function, which we can pass to findIndex.

a = {x: 1};
b = {x: 2};
arr = [a, b];

Now we want to remove c, which is equal to, but not the same as a:

c = {x: 1}

index = arr.findIndex(
  (i) => i.x === c.x
)

if (index !== -1) {
  arr.splice(
    index, 1
  );
}    

Note that I'm passing an ES6 style lambda function here as the first parameter to findIndex.

Unless you have the luxury of only writing for evergreen browsers, you'll probably want to be using a transpiler like Babel to use this solution.

Solution with Immutability

If you care about immutability, and you want to return a new array, you can do it with filter:

a = {x: 1};
b = {x: 2};
arr = [a, b];

Let's keep everything that isn't a:

newArr = arr.filter((x) => x != a)
Freddy Daniel
  • 602
  • 11
  • 25
superluminary
  • 47,086
  • 25
  • 151
  • 148
16

Try:

var ar = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}];

for(var i=0; i < ar.length; i++) {
   if(ar[i].value == "14" && ar[i].label == "7")
   {
      ar.splice(i,1);
   }
}

demo

Edgar Orozco
  • 2,722
  • 29
  • 33
3

If you dont want to mutate your array simply use filter method , Array.prototype.filter()

array.filter((i)=> i != "b");

Jimmy Obonyo Abor
  • 7,335
  • 10
  • 43
  • 71
1

I would create a new array...

var original = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]

var result = [];

for (var i = 0, l = original.length; i < l; i++) { // Traverse the whole array
  var current = original[i];
  if (! (current.value == 14 && current.label == 7) ) {
    // It doesn't match the criteria, so add it to result
    result.push( current );
  }
}

Edit: I've read your question once more. You only want to remove the first element? Then use slice to get only a part of the array.

var result = original.slice(1, original.length)

or splice

original.splice(0,1); // Remove one element from index 0.
1

Here is a way to do it without 'creating a new array' or using external libraries. Note that this will only remove the first instance found, not duplicates, as stated in your question's example.

// Array to search
var arr = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}];

// Object to remove from arr
var remove = {"value":"14","label":"7"};

// Function to determine if two objects are equal by comparing
// their value and label properties
var isEqual = function(o1, o2) {
    return o1 === o2 || 
        (o1 != null && o2 != null && 
         o1.value === o2.value &&
         o1.label === o2.label);
};

// Iterate through array and determine if remove is in arr
var found = false;
var i;
for(i = 0; i < arr.length; ++i) {
    if(isEqual(arr[i], remove)) {
        found = true;
        break;
    }
}

if(found) {
    // Found remove in arr, remove it
    arr.splice(i, 1);
}

Demo

datchung
  • 3,778
  • 1
  • 28
  • 29
1

Using ES6 approach:

const objIndex = objs.findIndex(o => o.value === '14' && o.label === '7')

if(objIndex > -1) {
  objs.slice(objIndex, 1)
}
José Antonio Postigo
  • 2,674
  • 24
  • 17
0

Do you need to keep the duplicate object?

With underscoreJS

var oldarray = [{"value":"14","label":"7"},{"value":"14","label":"7"},{"value":"18","label":"7"}]
var newarray = _.reject(oldarray, function(o) { 
  return o == {"value":"14","label":"7"}; 
});
Kang
  • 11
0
function removeDuplicateObject (arr) {
  let temp = arr.map((item) => {
    return JSON.stringify(item);
  });
  temp = Array.from(new Set(temp));
  return temp.map((item) => {
    return JSON.parse(item);
  });
}

Then removeDuplicateObject(your array)