从一行阵列变换阵列到多个行阵列

问题描述:

我有返回这样的值的数组:从一行阵列变换阵列到多个行阵列

0:1,2,3,4

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

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

如果即时通讯使用JavaScript做,我该怎么办呢?

+0

输入是'array'([1,2,3,4])或'object'({0:[1,2,3,4]})? – Rayon

+0

它是一个对象 – user3241620

有了一个循环,例如。

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

+1

太棒了!谢谢 – user3241620

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

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 - [发表评论](/ review/low-quality-posts/11505767) –

+0

@DanielCheung现在检查 – gurvinder372

你可以这样做:

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

+0

我得到这个错误:Uncaught TypeError:sortOrderValues [0] .split不是函数 – user3241620

+0

你可以做console.log(sortOrderValues )并给我输出,谢谢 – vvo