Javascript equivalent of Python’s values() dictionary method [duplicate]

Updated
I’ve upvoted Adnan’s answer as it was the first. I’m just posting a bit more details if it helps.

The for..in loop is what you are looking for –

var dictionary = {
    id:'value',
    idNext: 'value 2'
}

for (var key in dictionary){
    //key will be -> 'id'
    //dictionary[key] -> 'value'
}

To get all the keys in the dictionary object, you can Object.keys(dictionary)
This means, you can do the same thing in an array loop —

var keys = Object.keys(dictionary);
keys.forEach(function(key){
    console.log(key, dictionary[key]);
});

This proves especially handy when you want to filter keys without writing ugly if..else loops.

keys.filter(function(key){
    //return dictionary[key] % 2 === 0;
    //return !key.match(/regex/)
    // and so on
});

Update
To get all the values in the dictionary, currently there is no other way than to perform a loop. How you do the loop is a matter of choice though. Personally, I prefer

var dictionary = {
    a: [1,2,3, 4],
    b:[5,6,7]
}
var values = Object.keys(dictionary).map(function(key){
    return dictionary[key];
});
//will return [[1,2,3,4], [5,6,7]]

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)