1

I have an array like this:

var array=[
    ['0000'],
    ['0000']
]

and I'd like to get a result like this:

var result=[
    [0,0,0,0],
    [0,0,0,0]
]

How is this possible with javascript map?

My solution with a stupid for loop looks like this:

var array=[
    ['0000'],
    ['0000']
]

var new_array=[]

for (var i=0; i<array.length;i++) {
  new_array.push((array[i].toString().split('').map(Number)))
}
console.log(new_array)

Edit 1.0 How to get the same result if my array looks like this:

['0000','0000','0000']

Edit 2.0 Ok last edit - How to turn around the process? So getting from
[[0,0,0,0], [0,0,0,0]] -- > [['0000'], ['0000']]


EDIT 3 -- OK found the solution for Edit 2 on my own:

array.map(a => [a.join('')])

Thanks for your help.

Jonas

2
  • Something like this: var new_array = array.map(x => x[0].split("").map(Number))? Commented Nov 10, 2017 at 17:34
  • 1
    What is issue with using for loop? Commented Nov 10, 2017 at 17:40

3 Answers 3

1

To handle the following scenario...

var arrays=[
  ['0000', '0000', '0000'],
  ['0000', '0000', '0000'],
  ['0000', '0000', '0000'],
  // etc...
];

arrays.map(ary => ary.reduce((ary, stg) => ary.concat(stg.split('').map(Number)), []));
// => [[0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0, etc...], etc...];

A little shorter based on answer from @Josh from Qaribou

arrays.map(ary => ary.reduce((ary, stg) => ary.concat(Array.from(stg, Number)), []));
Sign up to request clarification or add additional context in comments.

2 Comments

Also the reduce function looks nice. Thanks for your help. Btw if you like to help me again look at my edit above. :)
Would the reverse require ['0000', '0000'] to this [0,0,0,0,0,0,0,0] and back to original? Or just back to a string like ['00000000'];
0

Like this. Give the logic with the current item as a function into the map function.

var array = [
    ['0000'],
    ['0000']
];

var mappedArray = array.map(item => item[0].split('').map(item => parseInt(item)));

console.log(mappedArray);

Edit 1.0

var array = [
    '0000',
    '0000'
];

var mappedArray = array.map(item => item.split('').map(item => parseInt(item)));

console.log(mappedArray);

Edit 2.0

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

var mappedArray = array.map(item => [item.join('')]);

console.log(mappedArray);

6 Comments

@Jonas0000 In this answer, the .map(item => parseInt(item)) accomplishes the same thing, although it could be shortened to just .map(parseInt).
I have used another way :D
@CRice. Sure thats not the problem. He already edited his post a few seconds ago because in the first version of the answer he forget to add the parseInt function.
Thanks for your help but please look at my edit above. Greetings
You need just to remove the [0] part from the above code
|
0

Trivially short after your edit:

const array = ['0000', '0000'];
const new_array = array.map(str => Array.from(str, Number));

// [[0,0,0,0],[0,0,0,0]]

Pre-edit:

const array = [['0000'], ['0000']];
const new_array = array.map(([str]) => Array.from(str, Number))

Gets a bit more verbose if you need to be [['0000', '0000'], ['0000', '0000']] is also valid:

const array = [['0123','4567'],['8901','2345']];
const new_array = array.map(strs => strs.reduce((a, e) => (a + e), '')).map(str => Array.from(str, Number));

Comments

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.