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

171
Visualizações
Checkbox is not selected after comparison with an array

I have an array containing an object. I would like to have a checkbox marked when the object appears in the array. But my checkbox is not marked, see picture.Only the name is displayed as a chip in the select field. I do not know why

enter image description here

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

0

The js includes function is comparing items with the === operator and this doesn't work with objects.

for exmaple the following expression will return false :

{id:1} === {id:1}

To solve your problem you can use the js function some that check if one element of the array match the condition

To compare your object you can either write your logic in the some function or write another function to compare your items.

Note: In my opinion, in your case, you can only compare the identifier of the objects to check if they are included in the personNamen array

checked={personNamen.some(person => person.name === name.name)}

Small example

const personNamen = [{
  name: "Foo", id: 1
}]

const name = {
  name: "Foo", id: 1
}

const name2 = {
  name: "Bar", id: 2
}

const compareObjects = (obj1, obj2) => {
  return obj1.name === obj2.name && obj1.id === obj2.id
}

console.log(personNamen.some(person => compareObjects(person, name))) //true
console.log(personNamen.some(person => compareObjects(person, name2)))  //false


Alternating solution

Another solution would be to map the array to the value of the identifier and check if the id is included in the mapped array

Example :

const personNamen = [
  {id: 1, name: "Foo"},
  {id: 2, name: "Bar"},
]

const reduceArray = personNamen.map(x => x.id)

const name1 = {id:1, name: "Foo"}
const name2 = {id:3, name: "Baz"}

console.log(reduceArray.includes(name1.id)) //true
console.log(reduceArray.includes(name2.id)) //false

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