Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  5] [ 7]  / answers: 1 / hits: 68497  / 14 Years ago, wed, january 5, 2011, 12:00:00

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


More From » jquery

 Answers
33

Use Object.values it will return array.



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

[#94360] Tuesday, January 4, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
randall

Total Points: 492
Total Questions: 99
Total Answers: 103

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;