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

143
Visualizações
How to create array in array JS

It's my first steps in JS and I'm trying to solve one task I got but I have the mistake.

I create the array in array and change the value inside and when I use for loop it return the mistake:

My array

let arrOne= [[12,3], 431, 432, 3234,123,123,512,2];

arrOne[i]["0"]=13;
              ^

TypeError: Cannot create property '0' on number '0'

If you know how create array in array with "for" and put value there please help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

OP says:

I need to create array in array like that [[1,2,3],[4,5,6],[7,8,9]]

So with that in mind

  • You can flatten your array, so you can have all of your values in one array.
  • Then loop through the array by incrementing the loop index by 3.
  • Inside the loop you can push the flatten array but sliced, using slice

const arrayOne = [
  [12, 3], 431, 432, 3234, 123, 123, 512, 2
];
const arrayOneFlatten = arrayOne.flat();
const newArray = [];
const numberToGroup = 3

for (let index = 0; index < arrayOneFlatten.length; index += numberToGroup) {
  newArray.push(arrayOneFlatten.slice(index, index + numberToGroup));
}
console.log(newArray);

about 4 years ago · Juan Pablo Isaza 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