将数组从一行数组转换为多行数组



>我有一个返回如下值的数组:

0: 1,2,3,4

我需要它返回这样的数组值:

0: 1
1: 2
2: 3
3: 4

如果我用javascript来做,我该怎么做呢?

带有循环,例如。

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]} );

最新更新