我有一个这样的数组:
["ID LONGITUDE LATITUDE", "0 77.139305 28.795975", "2 77.308929 28.486877", "4 73.820680 18.464110"]
我想这样做:
var array= ["ID", "LONGITUDE", "LATITUDE", "0", "77.139305", "28.795975", "2", "77.308929", "28.486877", "4", "73.820680", "18.464110"]
怎么做?
答案 0 :(得分:2)
如果你真的有一个数组,这里是如何简洁地做到这一点:
var rows = ["ID LONGITUDE LATITUDE", "0 77.139305 28.795975", "277.308929 28.486877", "4 73.820680 18.464110"];
[].concat.apply([], rows.map(function(row) { return row.split(' '); }))
// => ["ID", "LONGITUDE", "LATITUDE", "0", "77.139305", "28.795975", "277.308929", "28.486877", "4", "73.820680", "18.464110"]
答案 1 :(得分:2)
请尝试以下代码:
var string_array = ["ID LONGITUDE LATITUDE", "0 77.139305 28.795975", "2 77.308929 28.486877", "4 73.820680 18.464110"];
var new_string_array = [];
for(var strings in string_array) {
var x = string_array[strings].split(" ");
for(var sub_string in x) {
new_string_array.push(x[sub_string]);
}
}
alert(new_string_array);
答案 2 :(得分:1)
可以使用reduce
["ID LONGITUDE LATITUDE", "0 77.139305 28.795975", "2 77.308929 28.486877", "4 73.820680 18.464110"].reduce(function(arr, item) {
item.split(' ').forEach(function(item) {
arr.push(item)
});
return arr;
}, [])