2015-10-14 36 views
-2

这是我的代码如何将数组数组转换为angularjs中的对象数组?

$scope.students=[]; 

$scope.students[[object Object][object Object]] 
       [0]{"id":"101","name":"one","marks":"67"} 
       [1]{"id":"102","name":"two","marks":"89"} 

我想转换成以下格式一样。

$scope.students=[{"id":"101","name":"one","marks":"67"},{"id":"102","name":"two","marks":"89"}] 

我试过使用.map函数但不工作,现在我想通过使用angularjs将数组数组转换为对象格式数组。

+0

,你说的部分“这是我的代码” ...下面的代码是不是有效的JavaScript。所以......你能告诉我们你的实际代码吗? – frosty

回答

0

如果students变量是二维阵列,如下图所示:

students = [[{"id":"101","name":"one","marks":"67"}, {"id":"102","name":"two","marks":"89"}]]

您可以通过对象的每个阵列做双循环来,然后把每个对象到一个数组要保存它成。下面是香草JavaScript,但我相信有不同的方法来做到这一点。

var students = []; 
var results = []; 

students = [[{"id":"101","name":"one","marks":"67"}, {"id":"102","name":"two","marks":"89"}]] 

console.log(students); //array of array 
console.log('-----'); 
console.log(students[0]); //array of object 
console.log('-----'); 
for (i = 0; i < students.length; i++) { 
    console.log(students[i]); //the array of object 

    for(var j=0; j < students[i].length; j++){ 
    results.push(students[i][j]); 
    } 
} 
console.log('--Results---'); 
console.log(results); 

还检查我的JSBin:https://jsbin.com/tareku/edit?html,js,console

相关问题