Javascript equivalent to Python () vocabulary method

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

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 dictionary values ​​in Javascript?

EDIT

In my original print example, it was not visible what I would like to do. I only need a list / array of values ​​in the dictionary.

I understand that I can iterate over a list and create a new list of values, but is there a better way?

+44
javascript
Jul 31 2018-12-12T00:
source share
8 answers

Update
I approved Adnan's answer as he was the first. I just post some details if that helps.

The contour for..in 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 a dictionary object, you can Object.keys(dictionary)
That means you can do the same in an array loop -

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

This is especially useful if 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 }); 

Refresh - To get all the values ​​in a dictionary, there is currently no other way but to run a loop. However, how you do the loop is a matter of choice. 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]] 
+58
Jul 31 '12 at 6:45
source share

With jQuery, there is quite a single line 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 }); 
+16
Aug 05 '13 at 16:23
source share

Do not try to say that any of the other answers is incorrect, but if you are not against using an external library, underscore.js has a method for exactly this:

 _.values({one: 1, two: 2, three: 3}); // returns [1, 2, 3] 
+9
Jul 23 '14 at 18:37
source share

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

 Object.values = Object.values || function(o){return Object.keys(o).map(function(k){return o[k]})}; 
+8
May 21 '14 at 10:37
source share

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]); } 
+6
Jul 31 2018-12-12T00:
source share

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]); } 
+3
Jul 31 2018-12-12T00:
source share

In ES6, which is 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.

It is also useful for golf code . Removing spaces around a for shortens it to 17 characters.

+1
Feb 09 '15 at 16:51
source share

if you want to get only values ​​using the following code:

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

you can get individual items by index value in an array of items.

0
Jan 08 '14 at 12:25
source share



All Articles