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

Transform Array from one row Array to multiple row array

我有一个数组,其中 return 的值如下:

0: 1,2,3,4

我需要它来 return 数组值,如下所示:

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

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

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

你可以这样做:

var array = ['1,2,3,4'];
console.log(array[0].split(','));

有一个循环,例如

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>');