1

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.

2
  • I have answered your question. Please take a look. Commented Nov 9, 2017 at 16:31
  • But your code is absolutely not the right solution for my question @JesseSchokker Commented Nov 9, 2017 at 16:41

1 Answer 1

4

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);

Sign up to request clarification or add additional context in comments.

4 Comments

Not exactly the desired output. Should be [ ["0000"], ["0000"], ... ]. a => [a.join('')] should do the trick
Thanks Nina. But thats not the exactly the solution I've searched for. I'd like to have get a 2d array like I've mentioned above. @Nina Scholz
@Jonas0000 Just wrap the a.join('') in brackets, as I mentioned in my edited comment above. That will give you the output you desire
Ahh. Sorry @mhodges. I've failed to see your comment :) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.