Javascript:将对象的对象转换为对象数组 [英] Javascript: Converting Object of Objects to Array of Objects
本文介绍了Javascript:将对象的对象转换为对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Polymer 1.0进行项目,我想使用 dom-repeat
列出来自Firebase 3.0的数据。
I'm working on a project using Polymer 1.0 and I want to use dom-repeat
to list data from Firebase 3.0.
在Firebase中,我有一个像这样的对象:
In Firebase I have an object of objects like this:
var objectofobjects = {
"-KR1cJhKzg9uPKAplLKd" : {
"author" : "John J",
"body" : "vfdvd",
"time" : "September 6th 2016, 8:11",
"title" : "vfvfd"
},
"-KR1cLZnewbvo45fDnEf" : {
"author" : "JJ",
"body" : "vfdvdvf",
"time" : "September 6th 2016, 8:11",
"title" : "vfvfdvfdv"
}
};
我希望将它转换为这样的对象数组:
and I want to convert it to an array of objects like this:
var arrayofobjects = [ { '-KR1cJhKzg9uPKAplLKd':
{ author: 'John J',
body: 'vfdvd',
time: 'September 6th 2016, 8:11',
title: 'vfvfd' },
'-KR1cLZnewbvo45fDnEf':
{ author: 'JJ',
body: 'vfdvdvf',
time: 'September 6th 2016, 8:11',
title: 'vfvfdvfdv' } } ];
推荐答案
你可以做到以这种简单的方式:
You can do it in this simple way:
var arrObj = [];
var obj = JSON.stringify(objectofobjects, function(key, value) {
arrObj.push(value);
})
console.log(arrObj);
输出
将是:
[{
'-KR1cJhKzg9uPKAplLKd': {
author: 'John J',
body: 'vfdvd',
time: 'September 6th 2016, 8:11',
title: 'vfvfd'
},
'-KR1cLZnewbvo45fDnEf': {
author: 'JJ',
body: 'vfdvdvf',
time: 'September 6th 2016, 8:11',
title: 'vfvfdvfdv'
}
}]
注意:您提到的输出不是有效的JSON数组。
Note: The output which you have mentioned is not a valid JSON array.
希望这应该有效。
这篇关于Javascript:将对象的对象转换为对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文