我是Javascript的新手。我有一个像这样的Javascript对象:
s = {"Toothless":"Dragon","Foo":"Bar"};
我需要将它转换为一系列数组,如下所示:
out = [["Toothless","Dragon"],["Foo","Bar"]];
这与Convert JavaScript array of 2 element arrays into object key value pairs中讨论的相反。 JQuery解决方案是可以接受的。
答案 0 :(得分:9)
您可以映射项目以实现此目的:
s = {"Toothless":"Dragon","Foo":"Bar"};
var out = Object.keys(s).map(function(data){
return [data,s[data]];
});
console.log(out);
答案 1 :(得分:4)
let s = {"Toothless":"Dragon","Foo":"Bar"};
let out = Object.entries(s);
你得到out
作为一个小数组的数组,
见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
答案 2 :(得分:1)
var s = {"Toothless":"Dragon","Foo":"Bar"};
var out = [];
for (var key in s){
out.push([key, s[key]]);
}
答案 3 :(得分:0)
使用jQuery试试这个。
{{1}}