Komal Kanpariya
Komal Kanpariya

Reputation: 13

How to compare two array object in javascript if it match want to update object in array1?

I have two array objects and want to compare them in such a way that when the symbol value in array1 matches the keyNumber in array2 then set the corresponding checked value in array1 to true.

I am using .map with both arrays but not able to get the desired result.

These are my two arrays:-

const array1 = [{
    symbol: '1',
    checked: false
  },
  {
    symbol: '2',
    alias: 'abc',
    checked: false
  },
  {
    symbol: '3',
    alias: 'def',
    checked: false
  }];

const array2= [{
  keyNumber: '1',
   type: 'number',
 },
 {
   keyNumber: '2',
   type: 'number'
  }];

Following is the code to get updated array:

 const array3 = array1.map(item => {
      return array2
        .map(key =>
          key.keyNumber === item.symbol
            ? { ...item, checked: true, type: key.type }
            : item
        )
        .reduce((obj, data) => data, {});
    });

console.log(array3);

I am getting only last key matched result instead of all.

 array3 = [{
    symbol: '1',
    checked: false
  },
  {
    symbol: '2',
    alias: 'abc',
    checked: true,
    type: 'number'
  },
  {
    symbol: '3',
    alias: 'def',
    checked: false
  }];

Expected output:

array3 = [{
    symbol: '1',
    checked: true,
    type:'number'
  },
  {
    symbol: '2',
    alias: 'abc',
    checked: true,
    type: 'number'
  },
  {
    symbol: '3',
    alias: 'def',
    checked: false
  }];

Upvotes: 0

Views: 181

Answers (2)

Rajneesh
Rajneesh

Reputation: 5308

You can also do this with reduce by concatenating the second array in the last. In this case you wont have to find the first array for every loop.

var array1 = [{ symbol: '1', checked: false }, { symbol: '2', alias: 'abc', checked: false }, { symbol: '3', alias: 'def', checked: false }]; 
var array2= [{ keyNumber: '1', type: 'number', }, { keyNumber: '2', type: 'number' }];

array1 = [...array1, ...array2];

var result = Object.values(array1.reduce((acc, {symbol, keyNumber, ...rest})=>{
  data1 = symbol || keyNumber;
  acc[data1] = acc[data1] || {};
  if(symbol) acc[data1] = {symbol, ...rest};
  if(keyNumber) { acc[data1].type = rest.type, acc[data1].checked=true };
  return acc;
},{}));

console.log(result);

Upvotes: 0

Sifat Haque
Sifat Haque

Reputation: 6057

Here you can check my solution.

const newArray = array1.map(obj1 => {
  const match = array2.find(obj2 => obj1.symbol === obj2.keyNumber);
  if(match) {
    obj1 = {...obj1, checked: true, type: match.type}
  }
  return obj1;

})

Here is the full working code.

const array1 = [{
    symbol: '1',
    checked: false
  },
  {
    symbol: '2',
    alias: 'abc',
    checked: false
  },
  {
    symbol: '3',
    alias: 'def',
    checked: false
  }];

const array2= [{
  keyNumber: '1',
   type: 'number',
 },
 {
   keyNumber: '2',
   type: 'number'
  }];

const newArray = array1.map(obj1 => {
  const match = array2.find(obj2 => obj1.symbol === obj2.keyNumber);
  if(match) {
    obj1 = {...obj1, checked: true, type: match.type}
  }
  return obj1;
    
})


console.log(newArray)

Upvotes: 3

Related Questions