90

In Python I can use the .values() method to iterate over the values of a dictionary.

For example:

mydict = {'a': [3,5,6,43,3,6,3,],
          'b': [87,65,3,45,7,8],
          'c': [34,57,8,9,9,2],}
values = mydict.values():

Where values contains:

[
    [3,5,6,43,3,6,3,],
    [87,65,3,45,7,8],
    [34,57,8,9,9,2],
]

How can I get only the values of the dictionary in Javascript?

My original print example wasn't clear on what I'd like to do. I only want a list/array of the values within the dictionary.

I realize I can cycle through the list and create a new list of the values, but is there a better way?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
monkut
  • 42,176
  • 24
  • 124
  • 155
  • @Oriol I disagree with this closure. This one specifically asks about returning to an array. Granted, not all the answers address that properly. The other question wants to access it from a loop. I think this is shown by the fact that [my answer](http://stackoverflow.com/a/28414954/3187556) applies to this question but not the target. – Scimonster Feb 09 '15 at 17:03
  • @Scimonster The [most upvoted answer](http://stackoverflow.com/a/16643074/1529630) in the other question answers this perfectly. It even includes a variation of your answer to this question. You can vote to reopen if you disagree, though. – Oriol Feb 09 '15 at 17:07

8 Answers8

109

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]]
Jibi Abraham
  • 4,636
  • 2
  • 31
  • 60
38

Object.values() is available in Firefox 47 and Chrome 51, here's a one-line polyfill for other browsers:

Object.values = Object.values || function(o){return Object.keys(o).map(function(k){return o[k]})};
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
user
  • 23,260
  • 9
  • 113
  • 101
27

With jQuery, there's a pretty one line version using $.map():

var dict = {1: 2, 3: 4};
var values = $.map(dict, function(value, key) { return value });
var keys = $.map(dict, function(value, key) { return key });
XPX-Gloom
  • 601
  • 5
  • 11
Temuz
  • 1,413
  • 4
  • 14
  • 23
13

Not trying to say that any of the other answers are wrong, but if you're not opposed to using an external library, underscore.js has a method for precisely this:

_.values({one: 1, two: 2, three: 3});
// returns [1, 2, 3]
CatShoes
  • 3,613
  • 5
  • 29
  • 43
6

You can use for in

mydict = {'a': [3,5,6,43,3,6,3,],
          'b': [87,65,3,45,7,8],
          'c': [34,57,8,9,9,2]};
for (var key in mydict){
    alert(mydict[key]);
}
Adi
  • 5,089
  • 6
  • 33
  • 47
5

In ES6, currently supported by default in Firefox and with flags in Chrome, you can do this:

a = {'a': [3,5,6,43,3,6,3,],
      'b': [87,65,3,45,7,8],
      'c': [34,57,8,9,9,2]}
values = [a[x] for (x in a)];

values will now be the expected array.

This is also useful for code golf. Removing the spaces around for cuts it down to 17 characters.

Community
  • 1
  • 1
Scimonster
  • 32,893
  • 9
  • 77
  • 89
3

In javascript, you use for..in to loop the properties of an object.

var mydict = {
    'a': [3,5,6,43,3,6,3,],
    'b': [87,65,3,45,7,8],
    'c': [34,57,8,9,9,2]
 };

for (var key in mydict) {
  console.log(mydict[key]);
}
xdazz
  • 158,678
  • 38
  • 247
  • 274
1

if you want to get only the values the use the follwing code:

 for(keys in mydict){
   var elements = mydict[keys];
   console.log(elements);
 }

you can get Individual elements by index value in elements array.

BeingNerd
  • 115
  • 2
  • 5
  • 12