I am trying to split (in Vanilla JS) a string into a single or multiple arrays after each \n character.
For example :
let string = '1 2 3 4\n5 6 7 8\n9 8 7 6';
should return :
let combinedArrays = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 8, 7, 6]];
by splitting the let string after every \n character and deleting all the space characters.
Thanks for your help.
-
Using split js functiondanieltc07– danieltc072020年06月03日 20:01:27 +00:00Commented Jun 3, 2020 at 20:01
1 Answer 1
let combinedArrays = string.split('\n').map(el => el.split(' '))
let string = '1 2 3 4\n5 6 7 8\n9 8 7 6';
console.log(string.split('\n').map(el => el.split(' ')))
Please read more about split() method here: https://developer.mozilla.org/uk/docs/Web/JavaScript/Reference/Global_Objects/String/split
answered Jun 3, 2020 at 20:01
shutsman
2,5101 gold badge17 silver badges26 bronze badges
Sign up to request clarification or add additional context in comments.
lang-js