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

99
Views
Javascript 2D array filled with loop

I am trying to make a 2D array javascript using looping, with a result like this:

[[a1,a2,a3,a4,a5][b1,b2....]..[...e3,e4,e5]]

I have tried multiple different things, using for loops and the below, but am unable to create as needed.

var cols = [1,2,3,4,5];
var rows = ["a", "b", "c","d", "e"];

var grid = [ for (r of rows) [ for (c of cols) r+c ] ];

any help is appreciated.

Thanks

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can get your desired result using a nested map over the rows and columns:

var cols = [1,2,3,4,5];
var rows = ["a", "b", "c","d", "e"];

var grid = rows.map(r => cols.map(c => r + c));

console.log(grid)

about 4 years ago · Juan Pablo Isaza Report

0

It looks like @Nick beat me to it again ;-) but, obviously, this is the solution:

var cols = [1,2,3,4,5];
var rows = ["a", "b", "c","d", "e"];

const res = rows.map(r=>cols.map(c=>r+c));

console.log(res);

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!