Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
Add key value to an object array if it has same key value

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,
  },
 ];
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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).

about 4 years ago · Juan Pablo Isaza Relatório

0

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);

about 4 years ago · Juan Pablo Isaza Relatório

0

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; }

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda