Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object.values() in jQuery

The prototypeJS library has a method Object.values() which returns an array of values in an object.

EG:

 var myObj = {
   "key1" : "val1"
   "key2" : "val2"
 }
 Object.values(myObj) //returns ["val1", "val2"]

is there a jQuery method that does the same thing?

like image 611
indieman Avatar asked Feb 09 '13 21:02

indieman


1 Answers

I don't think there's a method that does it directly, but you can use $.map():

$.map(myObj, function(val, key) { return val; }); //returns ["val1", "val2"]

(Note though that if the callback returns null or undefined for a given property that item will not be included in the new array, so if your object might have properties with those values you'll have to do it another way. It's pretty easy to code from scratch though, with a for..in loop.)

like image 126
nnnnnn Avatar answered Oct 12 '22 23:10

nnnnnn