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

88
Visualizações
Updating single property values in a multidimensional array

I have the 1 dimension array called [cars] and values are added through an input box using React JS.

//initial array setup
const cars = [ ];
const [itemsList, setItemsList] = useState([ {brand: ""} ]);
 
//function called every time I add a new value
 
const carsUpdate = (e, index) => {     
    const list = [...itemsList];
    list[index]["brand"] = e.target.value;
    setItemsList(list);
  };

After adding 3 cars I get the following.

const cars = [
         { brand: “Fiat” },
         { brand: “Ford” },
         { brand: “Renault” }
];

The above works perfectly for 1 a dimension array type.

  Now, I’m trying to make it multi-dimension by adding an extra property [colors] to the array which contains another array   The idea is to get this type of result by adding colors separately and individually through other input boxes.

const cars = [
         { brand: “Fiat”, colors: [ “red”, “green” ] },
         { brand: “Ford”, colors: [ “blue”, “yellow” ] },
         { brand: “Renault”, colors: [ “red”, “white” ] },
]

  I have modified the following:

const [itemsList, setItemsList] = useState([ {brand: "", colors: [ ] } ])

but I’m struggling to modify the function carsUpdate to update the colors property with new values.

const carsUpdate = (e, index) => {     
    let list = [...itemsList];
    let innerList = [];
    innerList = list[index]["colors"]; 
    innerList.push(e.target.value);
    list[index][colors] = innerList;
    setItemsList(list);
  };
about 4 years ago · Juan Pablo Isaza
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