Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Remove object from array of objects

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"}]

?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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); });
over 4 years ago · Santiago Trujillo Report

0

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)
over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!