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

140
Visualizações
Change object value in array if the object id has own property in other object id

this is my data

let a = [
  {
    "activityCode": "23",
    "isValid": null
  },
  {
    "activityCode": "28",
    "isValid": null
  },
  {
    "activityCode": "32",
    "isValid": null
  }
]

let b = [
  {
    "activityCode": "23",
    "allocated": 3
  },
  {
    "activityCode": "32",
    "allocated": 2
  }
]

i want to change "isValid" to true if activityCode(a) in array has the same activityCode(b) & "isValid" to false if activityCode(a) in array has not the same activityCode(b), and the output i need like this:

let a = [
  {
    "activityCode": "23",
    "isValid": true
  },
  {
    "activityCode": "28",
    "isValid": false
  },
  {
    "activityCode": "32",
    "isValid": true
  }
]

Please help me. thanks guys.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

a.map(aa => {
  const match = b.find(bb => bb.activityCode === aa.activityCode);
  return { activityCode: aa.activityCode, isValid: match !== undefined };
})

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this :

a.forEach(element => {
  element.isValid = b.some(e => e.activityCode === element.activityCode);
});
console.log(a);
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.map() and Array.some() to get the desired result. If any element from a is present in array b Array.some() will return true.

let a = [ { "activityCode": "23", "isValid": null }, { "activityCode": "28", "isValid": null }, { "activityCode": "32", "isValid": null } ]
let b = [ { "activityCode": "23", "allocated": 3 }, { "activityCode": "32", "allocated": 2 } ]

const result = a.map(({ activityCode }) => ({ activityCode, isValid: b.some(el => el.activityCode === activityCode ) }));
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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