[英]JavaScript, transform object into array
I've got an object:我有一个对象:
var obj = {
"Mike": 24,
"Peter": 23,
"Simon": 33,
"Tom": 12,
"Frank": 31
};
I want to create an array that holds the values of the object.我想创建一个保存对象值的数组。 The keys (key names) can be disregarded:
可以忽略键(键名):
[24, 23, 33, 12, 31]
The order of the values is NOT important!值的顺序并不重要!
One solution (obviously) would be do have a function that takes the values and puts them into an array:一个解决方案(显然)是有一个函数来获取值并将它们放入一个数组中:
var arr = valuesToArray(obj);
I will accept such a function as the answer.我会接受这样的功能作为答案。 However, I would be more pleased if there would be an API function (ECMAScript, jQuery, browser-specific, ...) that could do this.
但是,如果有一个 API 函数(ECMAScript、jQuery、特定于浏览器,...)可以做到这一点,我会更高兴。 Is there such a thing?
有这样的事情吗?
The obvious way would be to do a for-in loop, as @quixoto suggests, but just for the record, and since you are looking for a built-in way, you could pair the new ECMAScript 5 methods Object.keys
and Array.prototype.map , available on latest browsers :显而易见的方法是执行 for-in 循环,正如@quixoto 所建议的那样,但只是为了记录,而且由于您正在寻找一种内置方式,您可以将新的 ECMAScript 5 方法
Object.keys
和Array配对。 prototype.map ,可在最新浏览器上使用:
function valuesToArray(obj) {
return Object.keys(obj).map(function (key) { return obj[key]; });
}
UPDATE : ES2017 introduced the Object.values
method, which does exactly what you want.更新:ES2017 引入了
Object.values
方法,它完全符合您的要求。
Additionally, ES2017 adds another often useful method, Object.entries
.此外,ES2017 添加了另一个常用方法
Object.entries
。 This method returns an array of key-value pairs .此方法返回一个键值对数组。
const obj = { "Mike": 24, "Peter": 23, "Simon": 33, "Tom": 12, "Frank": 31 }; const values = Object.values(obj); const entries = Object.entries(obj); console.log('values:', values); console.log('entries:', entries);
使用Object.values它将返回数组。
Object.values(obj) // [24, 23, 33, 12, 31]
There's no built-in way to do this anywhere.没有内置的方法可以在任何地方执行此操作。 The following does what you suggest, and may be "shortened" into more clever functional-programming versions depending on your library, but they'll all have the same efficiency.
以下按照您的建议进行操作,并且可能会根据您的库被“缩短”为更聪明的函数式编程版本,但它们都具有相同的效率。
function valuesToArray(obj) {
var result = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
result.push(obj[key]);
}
}
return result;
}
With jQuery you could use the each function:使用 jQuery,您可以使用 each 函数:
var obj = {
"Mike": 24,
"Peter": 23,
"Simon": 33,
"Tom": 12,
"Frank": 31
}
myArray=new Array();
$.each(obj, function(key, value) {
myArray.push(value);
});
Posting this strictly for fun.发布此严格为了好玩。 Save your downvotes.
保存您的反对票。 I'm not recommending it actually be used.
我不建议实际使用它。 ;o)
;o)
Example: http://jsfiddle.net/WGpXX/示例: http : //jsfiddle.net/WGpXX/
var arr = eval( '[' +
JSON.stringify(obj)
.slice(1,-1)
.replace(/"[^"]+":/g,'')
+ ']');
Technically works in this simple case.在这种简单的情况下,技术上可行。
Using the Underscore lib try:使用下划线库尝试:
function valuesToArray(o) {
return _.pairs(o);
}
var obj = {
"Mike": 24,
"Peter": 23
//...
},
result = valuesToArray(obj);
Then the result is [ ["Mike", 24], ["Peter", 23] ];那么结果是 [ ["Mike", 24], ["Peter", 23] ];
More detail on the pairs method here: http://underscorejs.org/#pairs关于pairs方法的更多细节: http : //underscorejs.org/#pairs
Try this:尝试这个:
var obj = { "Mike": 24, "Peter": 23, "Simon": 33, "Tom": 12, "Frank": 31 } ;
var arr = []
for(var a in obj)
{
var val = obj[a];
arr.push(val);
}
alert(arr.length)
Some libraries have something to do this (such as prototype's "values" function), but they're really just wrappers around a function that loops over and returns the values of the object.一些库可以做到这一点(例如原型的“值”函数),但它们实际上只是一个函数的包装器,该函数循环并返回对象的值。
http://www.prototypejs.org/api/object/values http://www.prototypejs.org/api/object/values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.