用javascript对象填充数组

时间:2015-06-01 14:54:33

标签: javascript arrays object

说我有以下对象:

var obj = Object {izaalberg: "65", jfm: "276", matcheanauto: "981", matchfullauto: "2525", mcgoncalves: "221"…}

我想将值对象添加到数组X中,所以结果是:

[['izaalberg',65], ['jfm',7 ],['matcheanauto:',981],['matchfullauto',2525],['mcgoncalves',221]...];

2 个答案:

答案 0 :(得分:2)

您需要简单的for循环

var result = [];
for(var i in obj){
    result.push([i, obj[i]]);
}

答案 1 :(得分:0)

使用for..in循环和Array.prototype.push

var obj = {
  izaalberg: "65",
  jfm: "276",
  matcheanauto: "981", 
  matchfullauto: "2525", 
  mcgoncalves: "221"
};

var arr = [];

for (var k in obj) {
  arr.push([k, obj[k]]);
}
            
console.log(arr)