Typical javascript array comparison and manipulation

jey

Joined
Jan 4, 2023
Messages
1
Reaction score
0
How to compare an array with another array object's key and then need to delete the resulting object in the third array.
for example, having 3 arrays like
let arr1 = [ {id:1,color:'blue'},{id:2,color:'red'},{id:3,color:'yellow'}];

let arr2 = [{id:2,color:'red'},{id:3,color:'yellow'}];
now want to check
1. the arr1 have the arr2 objects, arr2 object value color with arr1
2. remove the arr2 values in the arr1.
thanks in advance.
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
Since we haven't got any information about the third array, the Council of Zion decided to approve the code below:

JavaScript:
   /*
   now want to check
   1. the arr1 have the arr2 objects, arr2 object value color with arr1
   2. remove the arr2 values in the arr1.
   */
   
   let arr1 = [
   {id:1,color:'blue'},
   {id:2,color:'red'},
   {id:3,color:'yellow'}
   ];
  
   let arr2 = [
   {id:2,color:'red'},
   {id:3,color:'yellow'}
   ];
  
   const result = arr1.filter((obj1) => {
    return !arr2.find( obj2 => obj1.color === obj2.color );
   });
   console.log(result); // [{id: 1, color: 'blue'}]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top