Javascript:将对象的对象转换为对象数组

编程入门 行业动态 更新时间:2024-10-24 14:16:54
本文介绍了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:将对象的对象转换为对象数组

本文发布于:2023-11-30 12:11:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   数组   转换为   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!