Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

161
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda