简体   繁体   English

如何将 JavaScript object 的属性值提取到数组中?

[英]How might I extract the property values of a JavaScript object into an array?

Given a JavaScript object:给定 JavaScript object:

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

How do I efficiently extract the inner objects into an array?如何有效地将内部对象提取到数组中? I do not need to maintain a handle on the object[n] IDs.我不需要维护object[n] ID 的句柄。

var dataArray = [
    {id: 1, name: "Fred"}, 
    {id: 2, name: "Wilma"}, 
    {id: 3, name: "Pebbles"}]
var dataArray = Object.keys(dataObject).map(function(k){return dataObject[k]});
var dataArray = [];
for(var o in dataObject) {
    dataArray.push(dataObject[o]);
}

ES6版本:

var dataArray = Object.keys(dataObject).map(val => dataObject[val]);

使用下划线

var dataArray = _.values(dataObject);

With jQuery, you can do it like this -使用 jQuery,你可以这样做 -

var dataArray = $.map(dataObject,function(v){
     return v;
});

Demo演示

ES2017 using Object.values : ES2017 使用Object.values

 const dataObject = { object1: { id: 1, name: "Fred" }, object2: { id: 2, name: "Wilma" }, object3: { id: 3, name: "Pebbles" } }; const valuesOnly = Object.values(dataObject); console.log(valuesOnly)

Assuming your dataObject is defined the way you specified, you do this:假设您的 dataObject 是按照您指定的方式定义的,您可以这样做:

var dataArray = [];
for (var key in dataObject)
    dataArray.push(dataObject[key]);

And end up having dataArray populated with inner objects.并最终让 dataArray 填充了内部对象。

Using the accepted answer and knowing that Object.values() is proposed in ECMAScript 2017 Draft you can extend Object with method:使用已接受的答案并知道Object.values()是在ECMAScript 2017 草案中提出的,您可以使用方法扩展 Object:

if(Object.values == null) {
    Object.values = function(obj) {
        var arr, o;
        arr = new Array();
        for(o in obj) { arr.push(obj[o]); }
        return arr;
    }
}

[Editing and updating my answer. [编辑和更新我的答案。 The other answers seem to overlap with mine pretty much, but, I thought I have another ago and provide an alternative].其他答案似乎与我的几乎重叠,但是,我认为我以前有另一个答案并提供了替代方案]。

I present 3 solutions to this problem, based on:我针对这个问题提出了 3 个解决方案,基于:

  • Object.keys对象.keys
  • Object.values对象值
  • Object.entries对象条目

Objects.keys() solution: Objects.keys() 解决方案:

let keys = Object.keys(dataObject); // ["object1", "object2", "object3" ];
let keysToResult = keys.map( e => dataObject[e] ); // [{"id":1,"name":"Fred"},{"id":2,"name":"Wilma"},{"id":3,"name":"Pebbles"}]

Object.values solution: Object.values 解决方案:

let values = Object.values(dataObject); // [{"id":1,"name":"Fred"},{"id":2,"name":"Wilma"},{"id":3,"name":"Pebbles"}]

Object.entries solution: Object.entries 解决方案:

let entries = Object.entries(dataObject); // [["object1",{"id":1,"name":"Fred"}],["object2",{"id":2,"name":Wilma"}],["object3",{"id":3,"name":"Pebbles"}]]
let entriesToResult = entries.map( ([k,v]) => v ); [{"id":1,"name":"Fred"},{"id":2,"name":"Wilma"},{"id":3,"name":"Pebbles"}]

All three solutions have their own features.这三种解决方案都有自己的特点。

Object.keys() returns an array with insufficient result. Object.keys() 返回一个结果不足的数组。 So, we use Array.prototype.map to top up each value in the array to get close to what we want.因此,我们使用 Array.prototype.map 来填充数组中的每个值以接近我们想要的值。 In general, we can think of Object.keys() combined with map as a mechanism to customize our result list with what we want.通常,我们可以将 Object.keys() 与 map 结合视为一种机制,可以根据我们的需要自定义结果列表。

Object.values() is interesting since it discards the key and just returns the results only. Object.values() 很有趣,因为它丢弃键并且只返回结果。 In fact, for this problem, this is perfect since the answer requires no further processing.事实上,对于这个问题,这是完美的,因为答案不需要进一步处理。

Object.entries() returns more than what we want since it returns both keys and values. Object.entries() 返回的比我们想要的更多,因为它同时返回键和值。 We need to use map to customize our result.我们需要使用 map 来自定义我们的结果。 In fact, we need to cut out the excess information.事实上,我们需要剪掉多余的信息。

Object.keys(), Object.values() and Object.entries() are all very useful functions which is why I wanted to show all 3 as a solution to this problem. Object.keys()、Object.values() 和 Object.entries() 都是非常有用的函数,这就是为什么我想展示所有 3 个作为这个问题的解决方案的原因。 Depending on your specific use case, you may find one to a better fit to solving your problem.根据您的特定用例,您可能会找到更适合解决问题的方法。

Maybe a bit verbose, but robust and fast可能有点冗长,但强大且快速

var result = [];
var keys = Object.keys(myObject);
for (var i = 0, len = keys.length; i < len; i++) {
    result.push(myObject[keys[i]]);
}

Object.values() method is now supported.现在支持 Object.values() 方法。 This will give you an array of values of an object.这将为您提供一个对象值的数组。

Object.values(dataObject)

Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values参考: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

In case you use d3.如果您使用 d3。 you can do d3.values(dataObject) which will give你可以做d3.values(dataObject)这会给

在此处输入图片说明

I prefer to destruct object values into array:我更喜欢将对象值破坏为数组:

[...Object.values(dataObject)]

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

var dataArray = [...Object.values(dataObject)];

这个对我有用

var dataArray = Object.keys(dataObject).map(function(k){return dataObject[k]});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 JavaScript 中的嵌套数组对象中提取特定属性作为数组 - How can I extract specific property as an array from nested array object in JavaScript 如何将对象的属性值提取到数组中并使数组中的元素不同? - How to extract the property values of a object into an array and make elements in the array different? Javascript:如何将数组值添加到对象属性 - Javascript: How to add array values to object property 如何从 object 中提取引脚属性并在 JavaScript 中制作引脚数组? - How can I extract pin property from an object and make an array of pins in JavaScript? 如何从 javascript 中的 object 数组中提取特定属性(即键/值)? - how to extract particular property(i.e key/value) from array of object in javascript? 如何遍历并从复杂对象(数组)中提取属性值? - How to loop through and extract property values from a complex object (Array)? 如何通过从单独的数组中匹配的字符串对象值从 3 个嵌套的 json 数组中提取对象值,然后在 html/javascript 中显示它 - How can I extract object values from 3 nested json arrays by matched string object values from a separate array and then display it in html/javascript 如何使用 javascript 中的数组值更改数组 object 中的属性值 - How to change property value in array object with array values in javascript 如何将数组 Object 中的属性值替换为 Javascript 中另一个数组 Object 中的属性值 - How to Replace values of a property in an Array Object with values of a property in another Array Object in Javascript 如何从JSON返回嵌套在对象中的嵌套JavaScript数组的属性值? - How can I return the property values of a nested JavaScript array nested in an object from JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM