Javascript 如何在javascript字典中获取键的集合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10654992/
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
How to get collection of keys in javascript dictionary?
提问by FreeVice
I have dictionary:
我有字典:
var driversCounter = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5
}
Now, I need to show it in dropdownlist. How to get collection of keys in my dictionary?
现在,我需要在下拉列表中显示它。如何在我的字典中获取键的集合?
回答by alex
Use Object.keys()
or shimit in older browsers...
在旧浏览器中使用Object.keys()
或填充它...
const keys = Object.keys(driversCounter);
If you wanted values, there is Object.values()
and if you want key andvalue, you can use Object.entries()
, often paired with Array.prototype.forEach()
like this...
如果你想要values,有Object.values()
,如果你想要 key和value,你可以使用Object.entries()
,通常与Array.prototype.forEach()
这样配对......
Object.entries(driversCounter).forEach(([key, value]) => {
console.log(key, value);
});
Alternatively, considering your use case, maybe this will do it...
或者,考虑到您的用例,也许这会做到……
var selectBox, option, prop;
selectBox = document.getElementById("drivers");
for (prop in driversCounter) {
option = document.createElement("option");
option.textContent = prop;
option.value = driversCounter[prop];
selectBox.add(option);
}
回答by VisioN
One option is using Object.keys()
:
一种选择是使用Object.keys()
:
Object.keys(driversCounter)
It works fine for modern browsers (however, IE supports it starting from version 9 only).
它适用于现代浏览器(但是,IE 仅从版本 9 开始支持它)。
To add compatible support you can copy the code snippet provided in MDN.
要添加兼容支持,您可以复制MDN 中提供的代码片段。
回答by Joseph
to loop through the "dictionary" (we call it object in JS), use a for in
loop:
要循环遍历“字典”(我们在 JS 中称其为对象),请使用for in
循环:
for(var key in driversCounter) {
if(driversCounter.hasOwnProperty(key)) {
//key = keys, left of the ":"
//driversCounter[key] = value, right of the ":"
}
}
回答by Aadit M Shah
This will work in all JavaScript implementations:
这将适用于所有 JavaScript 实现:
var keys = [];
for (var key in driversCounter) {
if (driversCounter.hasOwnProperty(key)) {
keys.push(key);
}
}
Like others mentioned before you may use Object.keys
, but it may not work in older engines. So you can use the following monkey patch:
像之前提到的其他人一样,您可能会使用Object.keys
,但它可能不适用于较旧的引擎。所以你可以使用下面的猴子补丁:
if (!Object.keys) {
Object.keys = function (object) {
var keys = [];
for (var key in object) {
if (object.hasOwnProperty(key)) {
keys.push(key);
}
}
}
}
回答by Amil Sajeev
Simply use Object.keys()
:
只需使用Object.keys()
:
var driversCounter = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5
}
console.log(Object.keys(driversCounter));
回答by ThiefMaster
With a modern JS engine you can use Object.keys(driversCounter)
使用现代 JS 引擎,您可以使用 Object.keys(driversCounter)
回答by Parth Thakkar
for new browsers: Object.keys( MY_DICTIONARY )
will return an array of keys. Else you may want to go the old school way:
对于新浏览器:Object.keys( MY_DICTIONARY )
将返回一个键数组。否则你可能想走老派的路:
var keys = []
for(var key in dic) keys.push( key );
回答by Niet the Dark Absol
As others have said, you coulduse Object.keys()
, but who cares about older browsers, right?
正如其他人所说,您可以使用Object.keys()
,但谁在乎旧浏览器,对吗?
Well, I do.
嗯,我愿意。
Try this. array_keys
from PHPJS ports PHP's handy array_keys
function so it can be used in JS. At a glance, it uses Object.keys
if supported, but handles the case where it isn't very easily. It even includes filtering the keys based on values you might be looking for (optional) and a toggle for whether or not to use strict comparison ===
versus typecasting comparison ==
(optional)
试试这个。array_keys
从 PHPJS 移植了 PHP 的方便array_keys
功能,因此可以在 JS 中使用。一目了然,它使用Object.keys
if 支持,但处理它不是很容易的情况。它甚至包括根据您可能正在寻找的值过滤键(可选)和是否使用严格比较===
与类型转换比较的切换==
(可选)
回答by Darius Kucinskas
if you can use JQuery then
如果你可以使用 JQuery 那么
var keys = [];
$.each(driversCounter, function(key, value) {
keys.push(key);
});
console.log(JSON.stringify(keys));
here follows the answer:
下面是答案:
["one","two","three","four","five"]
and this way you wouldn't have to worry if the browser supports Object.keys
method or not.
这样你就不必担心浏览器是否支持Object.keys
方法。
回答by shiraz
A different approach would be to using multi-dimensional arrays:
另一种方法是使用多维数组:
var driversCounter = [
["one", 1],
["two", 2],
["three", 3],
["four", 4],
["five", 5]
]
and access the value by driverCounter[k][j], where j=0,1 in the case.
Add it in a drop down list by:
并通过 driverCounter[k][j] 访问该值,在这种情况下,j=0,1。
通过以下方式将其添加到下拉列表中:
var dd = document.getElementById('your_dropdown_element');
for(i=0;i<driversCounter.length-1;i++)
{
dd.options.add(opt);
opt.text = driversCounter[i][0];
opt.value = driversCounter[i][1];
}