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

176
Views
Modifying each value of array with immutability helper

I'm struggling to figure out what the best practice is to modify each value in an array of objects using the immutability helper.

For example, if I were to have this in my state:

this.state = {
    items: [
        {
            name: "test",
            subItems: [
                {val: false}, {val: true}, {val: false}
            ]
        },
        {
            name: "test2",
            subItems: [
                {val: true}, {val: true}, {val: false}
            ]
        }
    ]
}

And I want to set every val to false, how might I do that.

I could do it one at a time, but there must be a better way:

let elements = update(this.state.items, {
  [idx1]:{
    subItems:{
      [idx2]:{
        val: {
          $set: false
        }
      }
    }
  }
});
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

It certainly is possible, but it's everything but readable or understandable at first sight.

const nextState = update(state, {
    items: {
    $apply: (items) => {
        return items.map(item => {
        return update(item, {
            subItems: {
            $apply: (subItems) => {
                return subItems.map(subItem => {
                return update(subItem, {
                    val: {
                    $set: false
                  }
                })
              })
            }
          }
        })
      })
    }
  }
});
over 4 years ago · Santiago Trujillo Report

0

For an ES6 solution with immutability, destructuring and no helpers:

this.setState({
  // This is if you have other state besides items.
  ...this.state,
  items: this.state.items.map(item => ({
    ...item,
    subItems: item.subItems.map(subItem => ({
      ...subItem,
      val: false // or e.g. setter as a function of (item, subItem)
    }))
  }))
});

is easier on my eye than the update helper.

over 4 years ago · Santiago Trujillo Report

0

try this

this.state = {
    items: [
        {
            name: "test",
            subItems: [
                {val: false}, {val: true}, {val: false}
            ]
        },
        {
            name: "test2",
            subItems: [
                {val: true}, {val: true}, {val: false}
            ]
        }
    ]
}

use functional programming

this.state.items
.filter((item) => item.name === test).subitems
.map((subItem) => {
  subitem.forEach( (key) => {
   { [key]: !subitem[key] }

})
})
over 4 years ago · Santiago Trujillo 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!