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

191
Visualizações
How to set a value to true if an object exists in a list

I have a three lists, the first one is like this

[
  true,
  ['keyToOffice', false],
  false
]

The second one is like this:

[
  {
    reference: 'keyToOffice',
    a load of other irrelevant stuff
  },
  {
    reference: 'keyToHouse',
    a load of other irrelevant stuff
  }
]

The third one, my inventory is like this:

[
  'keyToOffice'
]

I would like to know if there is a way to update the second value of all the lists in the first list to true if the first item is inside of the third list. What would happen is that in ['keyToOffice', false], the false would become true because it is in the inventory list. If 'keyToOffice' was not in the inventory, then the false would stay false

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

0

If I understand your problem correctly, you don't need the second list. You can just iterate through the first list, and if the element is an array, and the string in it is included in the third list, change the boolean to true.

What map does is iterates through an array, and returns a one to one representation of that array (same number of elements), with certain elements changed according to the rules inside the function.

const firstList = [
  true,
  ['keyToOffice', false],
  false,
  true,
  ['keyToHouse', false],
  false,
  true,
  ['keyToCar', false],
  false,
]

const thirdList = [
  'keyToOffice',
  'keyToCar'
]

const firstListAmended = firstList.map(el => {
  if(Array.isArray(el)) {
    if(thirdList.includes(el[0])) {
        el[1] = true
    }
  }
  return el
})

console.log(firstListAmended)

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