JavaScript,将对象转换为数组 [英] JavaScript, transform object into array

查看:143
本文介绍了JavaScript,将对象转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象:

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]

值的顺序并不重要!

一个解决方案(很明显)会有一个函数来获取值并将它们放入一个数组中:

One solution (obviously) would be do have a function that takes the values and puts them into an array:

var arr = valuesToArray(obj); 

我会接受这样的功能作为答案。但是,如果有一个能够做到这一点的API函数(ECMAScript,jQuery,特定于浏览器......),我会更高兴。有这样的事吗?

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. Is there such a thing?

推荐答案

使用 Object.values 它会返回数组。

Object.values(obj) // [24, 23, 33, 12, 31]

这篇关于JavaScript,将对象转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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