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

275
Vistas
How do I update the value in object if the id matches?

How do I update the title in data if the id matches?

const data = {
    "0": {
        "id": 1912,
        "title": "Books",
    },
    "1": {
        "id": 1958,
        "title": "Brands",
    },
    "2": {
        "id": 2037,
        "title": "Logo",
    },
    "3": {
        "id": 2038,
        "title": "Colour",
    },
}

For example, This is the object with the latest updated title with the id: 1912.

const updatedData = {id: 1912, title: 'The Books'}

Sample output after updating data with updatedData

const data = {
    "0": {
        "id": 1912,
        "title": "The Books",
    },
    "1": {
        "id": 1958,
        "title": "Brands",
    },
    "2": {
        "id": 2037,
        "title": "Logo",
    },
    "3": {
        "id": 2038,
        "title": "Colour",
    },
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use Object.keys() to generate an array of the object keys ([0,1,2,3]). Loop through that array and check the id at each.

const data = {
    0: {
        id: 1912,
        title: 'Books',
    },
    1: {
        id: 1958,
        title: 'Brands',
    },
    2: {
        id: 2037,
        title: 'Logo',
    },
    3: {
        id: 2038,
        title: 'Colour',
    },
};

const updatedData = { id: 1912, title: 'The Books' };

Object.keys(data).forEach((el) => {
    if (data[el].id === updatedData.id) {
        data[el] = updatedData;
    }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

As for React, I'd suggest that you should mutate your object for the update.

You can use Object.entries to get keys (your indices) and values (your inner object which you want to update), and then use map to populate your updatedData on found data with updatedData.id

const data = {
    "0": {
        "id": 1912,
        "title": "Books",
    },
    "1": {
        "id": 1958,
        "title": "Brands",
    },
    "2": {
        "id": 2037,
        "title": "Logo",
    },
    "3": {
        "id": 2038,
        "title": "Colour",
    },
}

const updatedData = {id: 1912, title: 'The Books'}

const result = Object.entries(data).map(([key, value]) => value.id === updatedData.id ? { [key]: { ...value, ...updatedData } } : { [key]: value })

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