Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!