Javascript 等效于 Python 的 values() 字典方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11734417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:53:53  来源:igfitidea点击:

Javascript equivalent of Python's values() dictionary method

javascript

提问by monkut

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

在 Python 中,我可以使用该.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 valuescontains:

其中values包含:

[
    [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?

如何在 Javascript 中仅获取字典的值?

EDIT

编辑

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?

我意识到我可以循环浏览列表并创建一个新的值列表,但有没有更好的方法?

回答by Jibi Abraham

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

更新
我赞成 Adnan 的回答,因为它是第一个。如果有帮助,我只是发布更多细节。

The for..inloop is what you are looking for -

的for..in循环是你在找什么-

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 dictionaryobject, you can Object.keys(dictionary)
This means, you can do the same thing in an array loop --

要获取dictionary对象中的所有键,您可以Object.keys(dictionary)
这意味着,您可以在数组循环中做同样的事情——

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..elseloops.

当您想过滤键而不编写丑陋的if..else循环时,这证明特别方便。

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]]

回答by Temuz

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

使用 jQuery,有一个使用 $.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 });

回答by user

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

Object.values()在 Firefox 47 和 Chrome 51 中可用,这是其他浏览器的单行 polyfill:

Object.values = Object.values || function(o){return Object.keys(o).map(function(k){return o[k]})};

回答by CatShoes

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

并不是要说其他任何答案都是错误的,但是如果您不反对使用外部库,则underscore.js有一个方法可以做到这一点:

_.values({one: 1, two: 2, three: 3});
// returns [1, 2, 3]

回答by Adi

You can use for in

您可以使用 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]);
}

回答by xdazz

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

在 javascript 中,您使用for..in循环对象的属性。

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]);
}

回答by Scimonster

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

在 ES6 中,目前在 Firefox 中默认支持,在 Chrome 中使用标志,你可以这样做:

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)];

valueswill now be the expected array.

values现在将是预期的数组。

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

这对于代码高尔夫也很有用。删除周围的空格for将其减少到 17 个字符。

回答by BeingNerd

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.

您可以通过元素数组中的索引值获取单个元素。