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

142
Views
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 answers
Answer question

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 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!