admin管理员组文章数量:1421031
I have an array
var array = ['123.456789,123.1','456.7890123,234.1','789.0123456,345.1'];
The oute I'm looking for is
var array1 = [123.456789,456.7890123,789.0123456];
var array2 = [123.1,234.1,345.1];
What's best practice for doing this? I've been looking at .split(""); but would like to know the best way to approach it.
Thanks in advance Mach
I have an array
var array = ['123.456789,123.1','456.7890123,234.1','789.0123456,345.1'];
The oute I'm looking for is
var array1 = [123.456789,456.7890123,789.0123456];
var array2 = [123.1,234.1,345.1];
What's best practice for doing this? I've been looking at .split(""); but would like to know the best way to approach it.
Thanks in advance Mach
Share Improve this question asked Feb 21, 2013 at 9:34 MachMach 672 silver badges9 bronze badges3 Answers
Reset to default 3var arr = ["123.456789,123.1","456.7890123,234.1","789.0123456,345.1"];
var array1 = [],
array2 = arr.map(function(e) {
e = e.split(",");
array1.push(+e[0]);
return +e[1];
});
console.log(array1, array2);
I think that you should go with split function. Try this or see this DEMO:
var array = ['123.456789,123.1','456.7890123,234.1','789.0123456,345.1'];
var array1 = [], array2 = [];
for(var i=0; i<array.length; i++){
array1[i] = array[i].split(",")[0];
array2[i] = array[i].split(",")[1];
}
Basically you should iterate over the array and for each item you split the string into two parts, based on the ma. Each part goes into their respective array.
If Array.forEach()
is allowed:
var a1 = [], a2 = [];
array.forEach(function(item) {
var parts = item.split(',');
a1.push(+parts[0]);
a2.push(+parts[1]);
}
Otherwise:
var a1 = [], a2 = [];
for (var i = 0, item; item = array[i]; ++i) {
var parts = item.split(',');
a1.push(+parts[0]);
a2.push(+parts[1]);
}
本文标签: jquerySplit array into multiple array39s or alternateJavascriptStack Overflow
版权声明:本文标题:jquery - Split array into multiple array's or alternate - Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745334571a2653978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论