Jonas0000
Jonas0000

Reputation: 1113

Javascript create multiple strings from 2d array using MAP?

My array maybe looks like this:

var array=[
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0],
    [0,0,0,0]
]

How to get a result like this without using a simple for loop? (Using MAP constructor?!)

var result=[
    ['0000'],
    ['0000'],
    ['0000'],
    ['0000']
]

My for loops solution would be something like this, but is there a way to achieve the result without a for loop?

var array=[
  [0,0,0,0],
  [0,0,0,0],
  [0,0,0,0],
  [0,0,0,0]
]


var new_array=[]
for (var i=0; i<array.length; i++) 
  new_array.push(array[i].toString().replace(/,/g,''))

console.log(new_array)

Thanks in advance.

Upvotes: 1

Views: 49

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386680

You could map the joined values.

var array=[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],
    result = array.map(a => [a.join('')]);

console.log(result);

Upvotes: 4

Related Questions