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

68
Views
Remove equal objects from two arrays

I have the following problem in my ReactJs application. Let's say I have two arrays like this:

var cart = [
   {id: 1, name: "item1"}, 
   {id: 2, name: "item2"}, 
];
var productsArr = [
   {proId: 1, category: 'cat1'}, 
   {proId: 5, category: 'cat7'}, 
];

Is it possible to compare these 2 arrays and find any objects in productsArr which cart's id quals productsArr's proId and remove that object from only productsArr?

(If so, as I explained in this example, productsArr[0] should be removed.)

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use Array#filter in conjunction with Array#some.

var cart = [
   {id: 1, name: "item1"}, 
   {id: 2, name: "item2"}, 
];
var productsArr = [
   {proId: 1, category: 'cat1'}, 
   {proId: 5, category: 'cat7'}, 
];
productsArr = productsArr.filter(({proId})=>!cart.some(({id})=>proId === id));
console.log(productsArr);

about 4 years ago · Juan Pablo Isaza Report

0

var cart = [
   {id: 1, name: "item1"}, 
   {id: 2, name: "item2"}, 
];

var productsArr = [
   {proId: 1, category: 'cat1'}, 
   {proId: 5, category: 'cat7'}, 
];


for (var i = 0; i<productsArr.length; i++) {
    if (cart.find(item => item.id === productsArr[i].proId)) {
        productsArr.splice(i,1);
        i--;
    }
}

console.log(productsArr);

about 4 years ago · Juan Pablo Isaza 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!