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

169
Vistas
Can '=' be used to set one object property given a different unique key:value?

If I know an object exists in an array with a unique key:value pair do I have use .find() to get the object or is there a way that doesn't require iteration?

Given:

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'] }
]

Is there a notation to do:

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

Or do I have to do:

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

0

To directly answer your question...

Is there a notation to do

The answer is "no".

If your elements have unique IDs, considering collecting them into a Map keyed by id if you need that sort of access...

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; }

Because the object references in testObj and the Map are the same, any changes to one will be reflected in the other.

about 4 years ago · Juan Pablo Isaza Denunciar

0

As @Phil mentioned, the notation you asked about is not possible.

Another option is to use function .map() to return a new array with updated objects:

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