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

164
Vistas
¿Se puede usar '=' para establecer una propiedad de objeto dada una clave: valor única diferente?

Si sé que existe un objeto en una matriz con un par clave: valor único, ¿tengo que usar .find() para obtener el objeto o hay alguna forma que no requiera iteración?

Dado:

 const testObj = [ {id: '001', first: 'fThing1', other: [{id: '001.1'}, {id: '001.2'}], arr: ['a1', 'b1', 'c1'] }, {id: '002', first: 'fThing2', other: [{id: '002.1'}, {id: '002.2'}], arr: ['a2', 'b2', 'c2'] }, {id: '003', first: 'fThing3', other: [{id: '003.1'}, {id: '003.2'}], arr: ['a3', 'b3', 'c3'] } ]

¿Hay una notación para hacer:

 testObj.id['001'](some notation)first = 'something'

O tengo que hacer:

 temp = testObj.find(to => to.id === '001') temp.first = 'something'
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Para responder directamente a su pregunta...

¿Hay alguna notación que hacer?

La respuesta es "no" .

Si sus elementos tienen identificaciones únicas, considere recopilarlos en un Mapa codificado por id si necesita ese tipo de acceso...

 const testObj = [{"id":"001","first":"fThing1","other":[{"id":"001.1"},{"id":"001.2"}],"arr":["a1","b1","c1"]},{"id":"002","first":"fThing2","other":[{"id":"002.1"},{"id":"002.2"}],"arr":["a2","b2","c2"]},{"id":"003","first":"fThing3","other":[{"id":"003.1"},{"id":"003.2"}],"arr":["a3","b3","c3"]}] const idMap = new Map(testObj.map(o => [o.id, o])) // word of warning, this will error if the ID doesn't exist idMap.get("001").first = "something" console.log(testObj[0])
 .as-console-wrapper { max-height: 100% !important; }

Debido a que las referencias a objetos en testObj y Map son las mismas, cualquier cambio en uno se reflejará en el otro.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Como mencionó @Phil , la notación sobre la que preguntó no es posible.

Otra opción es usar la función .map() para devolver una nueva matriz con objetos actualizados:

 const testObj = [ {id: '001', first: 'fThing1', other: [{id: '001.1'}, {id: '001.2'}], arr: ['a1', 'b1', 'c1'] }, {id: '002', first: 'fThing2', other: [{id: '002.1'}, {id: '002.2'}], arr: ['a2', 'b2', 'c2'] }, {id: '003', first: 'fThing3', other: [{id: '003.1'}, {id: '003.2'}], arr: ['a3', 'b3', 'c3'] } ]; const result = testObj.map(item => item.id === '001' ? { ...item, first: 'something' } : item ); console.log(result);

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