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

237
Views
How to update the object key's value in state using useState() hook?

I have a array of objects state that has two key-value pairs [{name: 'Max', value: 1}]

when updating the state i am receiving the name from api and comparing with api.name == state.name. if it is true i need to update the value like value + 1. How do i achieve with setState()?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could use spread operator, to update the state by only changing the object properties.

this.setState({ array: this.state.array.map((item) => {
  const condition = "..."; // Implement logic
  return {...item, value: condition ? item.value + 1 : item.value }
 })
})
about 4 years ago · Juan Pablo Isaza Report

0

You could update the state like this:

    const stateUpdated = state.map((item) => {
      if (name === item.name) {
        return {
          ...item,
          value: ++item.value,
        };
      } else {
        return item;
      }
    });

    setState(stateUpdated);
about 4 years ago · Juan Pablo Isaza Report

0

You can update the value as:

//for example below data is coming from api

const array1 = [{ name: "Zee" }, { name: "Zee" }, { name: "Zee" }];

//Your state for updating value based on name

const [data, setData] = useState({ name: "Zee", value: 1 });

//check is name exist in api data or not if it exist it will increment the value

array1.map((item) => {
    if (item.name === data.name) {
      setData({ ...data, value: data.value + 1 });
    }
  });



console.log("check=", data);
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!