为数组中的每个条目拆分逗号

时间:2014-03-06 10:30:59

标签: javascript

任何更好的转换想法

["1","2","3,4","5","6","7,8,9"]["1","2","3","4","5","6","7","8","9"]

我想要实现的是创建少量代码,同时保持简单。

var arr=["1","2","3,4","5","6","7,8,9"];

var tmp=[];
for(var i=0,len=arr.length;i<len;i++){
  tmp=tmp.concat(arr[i].split(","));
}

http://plnkr.co/edit/iJG2aPZ3JAFvsulxJDQe?p=preview

我使用了简单的js,但你可以使用任何类型的库来减少代码。

2 个答案:

答案 0 :(得分:7)

使用join()并拆分。

arr.join(",").split(",");

Fiddle

答案 1 :(得分:4)

您可以就地进行替换:

for( var i=arr.length-1; i>=0; i--) {
    arr.splice.apply(arr,[i,1].concat(arr[i].split(",")));
}