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?
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With