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

135
Views
Update a element inside object in aobject array with Immutability helper js

I need to add or subtract a value of a element in object array using Immutability helper

const selectedFood= [
{id: uuidv4(), name: "pizza", price: 1700, amount: 3},
{id: uuidv4(), name: "cheese", price: 600, amount: 10},
{id: uuidv4(), name: "fried rice", price: 500, amount: 4},
{id: uuidv4(), name: "coke", price: 100, amount: 5}]


const [foodArray, setFoodArray] = useState(selectedFood);

This code works flawlessly and this is what i want except the index

setFoodArray((prev) => {
                 return update(prev, {2: {amount: {$apply: function(x) {return x -1}}}})
        })

the number 2 here should be the index

so i used this to get the index

const index = foodArray.indexOf(foodArray.find((single) => {
            return single.id === id
        }));

putting this index variable there dosn't work

setFoodArray((prev) => {
                 return update(prev, {index: {amount: {$apply: function(x) {return x -1}}}})
        })

Picture for understanding better

can anyone help

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

0

Use bracket notation:

setFoodArray((prev) => {
    return update(prev, {[index]: {amount: {$apply: function(x) {return x -1}}}})
})
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!