With
a = {2: "test", 3: "test2"};
console.log(Object.keys(a));
I get a list ["2", "3"].
Does there exists a function like Object.keys() for the values instead?
I have tried Object.values(a) but it doesn't work.
In JavaScript there is no values() method for objects. You can get values using a iteration, eg:
//using for ... in
var a = {2: "test", 3: "test2"};
var values = [];
for(var key in a){
values.push(a[key])
}
//Or just use `Array.map()` working with object keys:
var a = {2: "test", 3: "test2"};
var values = Object.keys(a).map(function(key){
return a[key]
});
Implementing Object.values():
Object.values = function(obj){
return Object.keys(obj).map(function(key){
return obj[key]
})
}
var a = {2: "test", 3: "test2"};
console.log(Object.keys(a));
console.log(Object.values(a));
collection2schema in Meteor. I save both the number and the name in my collection. Maybe I should avoid saving the name and always get the name from the dictionary :-D Thank you for making me realise