【发布时间】:2016-03-04 11:19:19
【问题描述】:
我有一个返回如下值的数组:
0: 1,2,3,4
我需要它返回这样的数组值:
0: 1
1: 2
2: 3
3: 4
如果我用javascript来做,我该怎么做?
【问题讨论】:
-
输入是
array([1,2,3,4]) 或object({0:[1,2,3,4]}) ?
标签: javascript arrays transform
我有一个返回如下值的数组:
0: 1,2,3,4
我需要它返回这样的数组值:
0: 1
1: 2
2: 3
3: 4
如果我用javascript来做,我该怎么做?
【问题讨论】:
array([1,2,3,4]) 或object({0:[1,2,3,4]}) ?
标签: javascript arrays transform
带有循环,例如。
var object = { '0': [1, 2, 3, 4] }, // the object
result = function (o) { // the iife for the result
var r = {}; // the temporary variable
o['0'].forEach(function (a, i) { // the loop over the property zero
r[i] = a; // the assignment to the object with
}); // the wanted key
return r; // the return of the temporary object
}(object); // the start with the object
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
【讨论】:
你可以这样做:
var array = ['1,2,3,4'];
console.log(array[0].split(','));
【讨论】:
i have an array that returns the values like this:
0: 1,2,3,4
试试这个
"0: 1,2,3,4".split(":").pop().split(",").map( function(value){return [value]} );
【讨论】: