Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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?

like image 534
monkut Avatar asked Jul 31 '12 06:07

monkut


People also ask

What is the equivalent of Python dictionary in JavaScript?

To write the equivalent of Python dictionary's get method in JavaScript, we can use hasOwnProperty . to call obj. hasOwnProperty with the keys we're looking for in obj .

Is there a dictionary in JavaScript?

Are there dictionaries in JavaScript? No, as of now JavaScript does not include a native “Dictionary” data type. However, Objects in JavaScript are quite flexible and can be used to create key-value pairs. These objects are quite similar to dictionaries and work alike.

Are JavaScript objects like Python dictionaries?

The data stored in the form of key-value pairs is called an Object or a Dictionary. Objects and dictionaries are similar; the difference lies in semantics. In JavaScript, dictionaries are called objects, whereas, in languages like Python or C#, they are called dictionaries.

Can 2 values have the same key in dictionary?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.


1 Answers

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]] 
like image 54
Jibi Abraham Avatar answered Oct 22 '22 00:10

Jibi Abraham