[英]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);
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 个解决方案,基于:
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
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.