2

I have an array

const myArr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ] 

I need to split into digits like this:

const splited = [ 9, 4, 9, 5, 9, 6, 9, 7, 9, 8, 9, 9, 1, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 5, 1, 0, 6 ] 

3 Answers 3

2

You could join the items, split and map numbers.

var array = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106], pieces = array.join('').split('').map(Number); console.log(pieces);

Same approach, different tools.

var array = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106], pieces = Array.from(array.join(''), Number); console.log(pieces);

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

1 Comment

Thank you for the fast answer. The second option is very nice, I will remember it.
1

map each number to a string and split the string, and spread the result into [].concat to flatten:

const myArr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ]; const splitted = [].concat(...myArr.map(num => String(num).split(''))); console.log(splitted);

Comments

0

You can use reduce function to create a new array and use split to split the number converted to string

const myArr = [94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106] let newArr = myArr.reduce(function(acc, curr) { let tempArray = curr.toString().split('').map((item) => { return +item; }); acc.push(...tempArray) return acc; }, []) console.log(newArr)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.