Home > OS >  How can I turn a string into multiple sub arrays in Javascript?
How can I turn a string into multiple sub arrays in Javascript?

Time:02-18

I would like to turn this:

"a:1,b:2,c:3"

Into:

  [['a', '1'],['b', '2'],['c', '3']]

CodePudding user response:

You could use the JavaScript split method here and do it twice. Something like the following:

let array = [];
let str = "a:1,b:2,c:3";
let splitStr = str.split(',');
splitStr.forEach(subStr => {
    array.push(subStr.split(':'));
});
  • Related