Convert string to multiple arrays in JavaScript

问题: I have a comma separated string and i want to convert it into multiple arrays. Here is the code i tried but failed to achieve the expected output. const data = "123,456";...

问题:

I have a comma separated string and i want to convert it into multiple arrays. Here is the code i tried but failed to achieve the expected output.

const data = "123,456";
result = data.split(',').map(s => Array.from(s));

console.log(result); // [["1", "2", "3"], ["4", "5", "6"]]

Expected output:

[["123"], ["456"]]

回答1:

Map it to an array.

const data = "123,456";
result = data.split(',').map(s => [s]);

console.log(result);


回答2:

As epascarello suggests on his comment, try

data.split(',').map(s =>[s]);

Take a look here for more info!

  • 发表于 2020-06-27 20:30
  • 阅读 ( 118 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除