如何将对象转换为Array数组

时间:2017-03-30 06:58:34

标签: javascript angularjs

我试过这样但是它不像我需要的那样

Object.keys(data).forEach(function (key) {
    console.log(data[key]);
    array = $.map(data[key], function(value, index) {
       return [value];
    }); 
});

输出我发现 -

0:{1: 2, 2: 2, 3: 7, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0}
1:{1: 3, 2: 1, 3: 2, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0}
2:{1: 0, 2: 2, 3: 2, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0}

请帮我看下面的结果:

0:[1: 2, 2: 2, 3: 7, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0]
1:[1: 3, 2: 1, 3: 2, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0]
2:[1: 0, 2: 2, 3: 2, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0]

提前致谢!

1 个答案:

答案 0 :(得分:1)

你只是缺少javasciprt的数组推送功能

var array = [], finarr = [];
Object.keys(data).forEach(function (key) {
    //console.log(data[key]);
    array = $.map(data[key], function(value, index) {
    return [value];
    });
    finarr.push(array); 
});

它会给出正确的结果我希望