• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

141
Vistas
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
        }
      }
    }
  }
});
about 3 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

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
                  }
                })
              })
            }
          }
        })
      })
    }
  }
});
about 3 years ago · Santiago Trujillo Denunciar

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.

about 3 years ago · Santiago Trujillo Denunciar

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] }

})
})
about 3 years ago · Santiago Trujillo 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda