Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split array element in two elements using javascript

I would like to do something which seems very easy but I haven't found the solution yet (I didn't find anything on the forum of course).

I want to separate one element of an array, in two.

For instance: I have an array

let array = [
  [ '201026 21330', '12385', '852589', '951478' ],
  [ '201026 21320', '12385', '369874' ]
]

I want to split the first element like that (notice the first elements, here separated):

let result = 
[
 [ '201026','21330', '12385', '852589', '951478' ],
 [ '201026','21320', '12385', '369874' ]
]

Does anybody have any clue?

Thanks in advance

like image 831
Mathieu Avatar asked Feb 16 '26 15:02

Mathieu


1 Answers

Use Array#map, String#split and Array#flat

const arr = [
  [ '201026 21330', '12385', '852589', '951478' ],
  [ '201026 21320', '12385', '369874' ]
]

const res = arr.map(row => row.map(item => item.split(' ')).flat());

console.log(res)
like image 94
kemicofa ghost Avatar answered Feb 18 '26 05:02

kemicofa ghost