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

167
Views
How to compare t re in the first array? JS

I have two arays. example of arrays:

[{id: 1, name: "test 1" , title : "test 11"} ,
{id: 2, name: "test 2" , title : "test 22"} ,
{id: 3, name: "test 3" , title : "test 3"} ,
{id: 4, name: "test 4" , title : "test 4}]

the difference between these two arrays is only the object located in the second array with id 4

this:

 {id: 4, name: "test 4" , title : "test 4}

What I need ?

I need to loop thought both array or whatever and remove all items which are in the first row.

After loop my neew array need to look like :

newArr = [{id: 4, name: "test 4" , title : "test 4}];

I will not write to you what I was trying to do because I will only confuse you.

EDIT: my try ->

         let concatArray = [...firstArray, ...secondArray];
         let uniqueItems = [...new Set(concatArray)];
         uniqueItems.filter((arr) => firstArray.id !== col.id);

This is no work...

merging two arrays I get nothing. I need to delete all items from the first array just.

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

0

We can use the Array filter function, and the findIndex function which allows us to find the index of an item in the array that meets a certain condition. If the item is not present, the index will be -1, and that's how we can identify the missing objects.

const filtered = secondArr.filter((secondItem) => {
    return firstArr.findIndex(({ id: firstItemId }) => firstItemId === secondItem.id) === -1;
}) 

console.log(filtered); // [ { id: 4, name: 'test 4', title: 'test 4' } ]
about 4 years ago · Juan Pablo Isaza Report

0

Lodash if you don't mind.

const firstArr =[{id: 1, name: 'test 1' , title : 'test 11'},{id: 2, name: 'test 2' , title : 'test 22'},{id: 3, name: 'test 3' , title : 'test 3'}];
const secondArr = [{id: 1, name: 'test 1', title : 'test 11'} ,{id: 2, name: 'test 2' , title : 'test 22'} ,{id: 3, name: 'test 3' , title : 'test 3'} ,{id: 4, name: 'test 4' , title : 'test 4'}];

const notInBoth = [
    ..._.differenceWith(firstArr, secondArr, _.isEqual),
    ..._.differenceWith(secondArr, firstArr, _.isEqual),
  ];
  
console.log(notInBoth);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

You can use some:

const firstArr =
  [{id: 1, name: 'test 1' , title : 'test 11'},
  {id: 2, name: 'test 2' , title : 'test 22'},
  {id: 3, name: 'test 3' , title : 'test 3'}];

const secondArr = [{id: 1, name: 'test 1', title : 'test 11'} ,
  {id: 2, name: 'test 2' , title : 'test 22'} ,
  {id: 3, name: 'test 3' , title : 'test 3'} ,
  {id: 4, name: 'test 4' , title : 'test 4'}];

let newArray = secondArr.filter(item => !firstArr.some(item2 => item2.id === item.id));

In spoken words: In newArray, look into secondArr and get me a list of items where they are not found in anywhere in firstArr.

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!