Home > Net >  Find similar items from 2 arrays of objects
Find similar items from 2 arrays of objects

Time:09-28

const array1 = [
{name: 'bc', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycb', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycn', module: 'ksd', cluster: 'opc2'}
]
const array2 = [
{name: 'bcl', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcc', service: 'ycb', module: 'ksd', cluster: 'opcw'},
{name: 'bca', service: 'ycb', module: 'ksd', cluster: 'opcm'},
{name: 'bcv', service: 'ycr', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycq', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycx', module: 'ksd', cluster: 'opc2'}
]

I would like to compare items with 2 keys: service module. If there are similar items, add key "type":"Multi", if not, add "type":"Single"

The result I expected:

result = [
{name: 'bc', service: 'yc', module: 'ksd', cluster: 'opc', type: 'Multi'},
{name: 'bcl', service: 'yc', module: 'ksd', cluster: 'opc', type: 'Multi'},
{name: 'bcc', service: 'ycb', module: 'ksd', cluster: 'opcw', type: 'Multi'},
{name: 'bca', service: 'ycn', module: 'ksd', cluster: 'opc2', type: 'Single'}
]

CodePudding user response:

//hope this would help you out

// Declare two array
    const array1 = ['a', 'b', 'c', 'd'];
    const array2 = ['k', 'x', 'z'];
 
// Function definition with passing two arrays
    function findCommonElement(array1, array2) {
     
    // Loop for array1
    for(let i = 0; i < array1.length; i  ) {
         
        // Loop for array2
        for(let j = 0; j < array2.length; j  ) {
             
            // Compare the element of each and
            // every element from both of the
            // arrays
            if(array1[i] === array2[j]) {
             
                // Return if common element found
                return console.log(array1[i]);
            }
        }
    }
     
    // Return if no common element exist
    return console.log("sorry no common element found");
    }

CodePudding user response:

const arr3 = [];
const array1 = [
{name: 'bc', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycb', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycn', module: 'ksd', cluster: 'opc2'}
]
const array2 = [
{name: 'bcl', service: 'yc', module: 'ksd', cluster: 'opc'},
{name: 'bcc', service: 'ycb', module: 'ksd', cluster: 'opcw'},
{name: 'bca', service: 'ycb', module: 'ksd', cluster: 'opcm'},
{name: 'bcv', service: 'ycr', module: 'ksd', cluster: 'opc'},
{name: 'bcx', service: 'ycq', module: 'ksd', cluster: 'opc1'},
{name: 'bca', service: 'ycx', module: 'ksd', cluster: 'opc2'}
]

array1.forEach((item)=>{
  array2.forEach((item2) => {
       if((item['module'] === item2['module']) && (item['service'] === item2['service'])){
           item.type = 'multi';
           arr3.push(item);
       }
  });
  if(!item.type) {
    item.type ='single';
    arr3.push(item);
  }
});

console.log(arr3);

you can optimize the solution using the higher order functions like filter, map, reduce, etc

  • Related