如何将 JavaScript 对象的属性值提取到数组中? [英] How might I extract the property values of a JavaScript object into an array?

查看:29
本文介绍了如何将 JavaScript 对象的属性值提取到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 JavaScript 对象:

Given a JavaScript object:

var dataObject = {
   object1: {id: 1, name: "Fred"}, 
   object2: {id: 2, name: "Wilma"}, 
   object3: {id: 3, name: "Pebbles"}
};

如何有效地将内部对象提取到数组中?我不需要维护 object[n] ID 的句柄.

How do I efficiently extract the inner objects into an array? I do not need to maintain a handle on the object[n] IDs.

var dataArray = [
    {id: 1, name: "Fred"}, 
    {id: 2, name: "Wilma"}, 
    {id: 3, name: "Pebbles"}]

推荐答案

var dataArray = [];
for(var o in dataObject) {
    dataArray.push(dataObject[o]);
}

这篇关于如何将 JavaScript 对象的属性值提取到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆