Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

180
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda