Home > Blockchain >  Add key value to an object array if it has same key value
Add key value to an object array if it has same key value

Time:06-10

I'm trying to add a property with a certain value to all of the objects in one array, based on a corresponding value in another array.

const array1 = [
  {
    id: 1,
    date: '2022.05.01',
    name: 'john'
  }, {
    id: 2,
    date: '2022.05.01',
    name: 'sam'
  }, {
    id: 3,
    date: '2022.05.03',
    name: 'john'
  }, {
    id: 4,
    date: '2022.05.06',
    name: 'jack'
  },
 ]
    

This array contains the required modifications that need to be made:

const array2 = [
  {
    name: 'john',
    isCanceled: true,
  }, {
    name: 'jack',
    isCanceled: false,
  }, {
    name: 'sam',
    isCanceled: false,
  },
 ]

If the name in the object within array1 is john then isCanceled should be set to true, but if it's jack or sam it should be set to false like so:

const resultArray = [
  {
    id: 1,
    date: '2022.05.01',
    name: 'john',
    isCanceled: true,
  }, {
    id: 2,
    date: '2022.05.01',
    name: 'sam'
    isCanceled: false,
  }, {
    id: 3,
    date: '2022.05.03',
    name: 'john'
    isCanceled: true,
  }, {
    id: 4,
    date: '2022.05.06',
    name: 'jack'
    isCanceled: false,
  },
 ];

CodePudding user response:

You can do this more simply by first converting your array2 into an object and then using a lookup within a map like so:

const array1 = [{id:1,date:"2022.05.01",name:"john"},{id:2,date:"2022.05.01",name:"sam"},{id:3,date:"2022.05.03",name:"john"},{id:4,date:"2022.05.06",name:"jack"},];

const array2 = [{name:"john",isCanceled:true},{name:"jack",isCanceled:false},{name:"sam",isCanceled:false}];

const arrCancel = array2.reduce((a, { name, isCanceled }) => {
  a[name] = isCanceled;
  return a;
}, {});

const resultArr = array1.map(e => {
  e.isCanceled = arrCancel[e.name];
  return e;
});

console.log(resultArr);
.as-console-wrapper { max-height: 100% !important; top: auto; }

CodePudding user response:

Build a status lookup object containing the status, and map your array to include the matching values from the lookup object:

const status = Object.fromEntries(
  array2.map(({name, isCanceled}) => [name, isCanceled])
);

const resultArray = array1.map(({id, date, name}) => ({
  id,
  date,
  name,
  isCanceled: status[name]
}));

Alternatively, if you just want to modify array1 instead of creating a new array, the second step can be replaced with:

array1.forEach(v => v.isCanceled = status[v.name]);

Complete snippet:

const array1 = [{
  id: 1,
  date: '2022.05.01',
  name: 'john'
}, {
  id: 2,
  date: '2022.05.01',
  name: 'sam'
}, {
  id: 3,
  date: '2022.05.03',
  name: 'john'
}, {
  id: 4,
  date: '2022.05.06',
  name: 'jack'
}, ];

const array2 = [{
  name: 'john',
  isCanceled: true,
}, {
  name: 'jack',
  isCanceled: false,
}, {
  name: 'sam',
  isCanceled: false,
}, ];

const status = Object.fromEntries(
  array2.map(({name, isCanceled}) => [name, isCanceled])
);

const resultArray = array1.map(({id, date, name}) => ({
  id,
  date,
  name,
  isCanceled: status[name]
}));

console.log(resultArray);

Should you wonder why I prefer to build the lookup object instead of using find() inside the map() operation: in terms of complexity, the lookup table has a complexity of O(1), while the find() approach is O(n).

CodePudding user response:

you can do something as :

const result = array1.map(person => {
  const lookPerson = array2.find(person2 => person2.name === person.name);
  return (lookPerson) ? { 
    ...person,
    isCanceled: lookPerson.isCanceled
  } : person;
});
  • combine array.map to transform array1

  • use array.find to get first iteration of similar person between array1 and array2

  • merge object with

    { 
      ...person,
      isCanceled: lookPerson.isCanceled
    }
    

const array1 = [{
  id: 1,
  date: '2022.05.01',
  name: 'john'
}, {
  id: 2,
  date: '2022.05.01',
  name: 'sam'
}, {
  id: 3,
  date: '2022.05.03',
  name: 'john'
}, {
  id: 4,
  date: '2022.05.06',
  name: 'jack'
}, ];

const array2 = [{
  name: 'john',
  isCanceled: true,
}, {
  name: 'jack',
  isCanceled: false,
}, {
  name: 'sam',
  isCanceled: false,
}, ];


const result = array1.map(person => {
  const lookPerson = array2.find(person2 => person2.name === person.name);
  return (lookPerson) ? { 
    ...person,
    isCanceled: lookPerson.isCanceled
  } : person;
});
console.log(result);

CodePudding user response:

You can do:

const array1 = [{id: 1,date: '2022.05.01',name: 'john'}, {id: 2,date: '2022.05.01',name: 'sam'}, {id: 3,date: '2022.05.03',name: 'john'}, {id: 4,date: '2022.05.06',name: 'jack'}]
const array2 = [{name: 'john',isCanceled: true,}, {name: 'jack',isCanceled: false,}, {name: 'sam',isCanceled: false,}]

const array2Hash = array2.reduce((a, { name: n, isCanceled: ic}) => (a[n] = ic, a), {})

const result = array1.map(u => ({
  ...u,
  isCanceled: array2Hash[u.name]
}))

console.log(result)

  • Related