在 JavaScript 中获取对象的键和值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10718473/
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 02:38:53  来源:igfitidea点击:

Get key and value of object in JavaScript?

javascriptjquery

提问by flossfan

Given a JavaScript array of objects, how can I get the key and value of each object?

给定一个 JavaScript 对象数组,如何获取每个对象的键和值?

The code below shows what I'd like to do, but obviously doesn't work:

下面的代码显示了我想做的事情,但显然不起作用:

var top_brands = [ { 'Adidas' : 100 }, { 'Nike' : 50 }];
var brand_options = $("#top-brands");
$.each(top_brands, function() {
  brand_options.append($("<option />").val(this.key).text(this.key + " "  + this.value));
});

So, how can I get this.keyand this.valuefor each entry in the array?

所以,我怎样才能得到this.keythis.value该阵列中的每个条目?

回答by Tomalak

Change your object.

改变你的对象。

var top_brands = [ 
  { key: 'Adidas', value: 100 }, 
  { key: 'Nike', value: 50 }
];

var $brand_options = $("#top-brands");

$.each(top_brands, function(brand) {
  $brand_options.append(
    $("<option />").val(brand.key).text(brand.key + " " + brand.value)
  );
});

As a rule of thumb:

根据经验:

  • An object has dataand structure.
  • 'Adidas', 'Nike', 100and 50are data.
  • Object keys are structure. Using data as the object key is semantically wrong. Avoid it.
  • 对象具有数据结构
  • 'Adidas''Nike'10050数据
  • 对象键是结构。使用数据作为对象键在语义上是错误的。躲开它。

There are no semantics in {Nike: 50}. What's "Nike"? What's 50?

中没有语义{Nike: 50}。什么是“耐克”?什么是50?

{key: 'Nike', value: 50}is a little better, since now you can iterate an array of these objects and values are at predictable places. This makes it easy to write code that handles them.

{key: 'Nike', value: 50}好一点,因为现在您可以迭代这些对象的数组,并且值位于可预测的位置。这使得编写处理它们的代码变得容易。

Better still would be {vendor: 'Nike', itemsSold: 50}, because now values are not only at predictable places, they also have meaningful names. Technically that's the same thing as above, but now a person would also understand what the values are supposed to mean.

更好的是{vendor: 'Nike', itemsSold: 50},因为现在值不仅位于可预测的位置,它们还有有意义的名称。从技术上讲,这与上述相同,但现在人们也会理解这些值的含义。

回答by thecodeparadox

$.each(top_brands, function() {
  var key = Object.keys(this)[0];
  var value = this[key];
  brand_options.append($("<option />").val(key).text(key + " "  + value));
});

回答by Graham

If this is all the object is going to store, then best literal would be

如果这就是对象要存储的全部内容,那么最好的文字是

var top_brands = {
    'Adidas' : 100,
    'Nike'   : 50
    };

Then all you need is a for...inloop.

那么你所需要的只是一个for...in循环。

for (var key in top_brands){
    console.log(key, top_brands[key]);
    }

回答by xdazz

$.each(top_brands, function(index, el) {
  for (var key in el) {
    if (el.hasOwnProperty(key)) {
         brand_options.append($("<option />").val(key).text(key+ " "  + el[key]));
    }
  }
});

But if your data structure is var top_brands = {'Adidas': 100, 'Nike': 50};, then thing will be much more simple.

但是如果你的数据结构是var top_brands = {'Adidas': 100, 'Nike': 50};,那么事情会简单得多。

for (var key in top_brands) {
  if (top_brands.hasOwnProperty(key)) {
       brand_options.append($("<option />").val(key).text(key+ " "  + el[key]));
  }
}

Or use the jquery each:

或者使用 jquery 每个:

$.each(top_brands, function(key, value) {
    brand_options.append($("<option />").val(key).text(key + " "  + value));
});

回答by Harish

for (var i in a) {
   console.log(a[i],i)
}

回答by Jakob

Object.keys(top_brands).forEach(function(key) {
  var value = top_brands[key];
  // use "key" and "value" here...
});

Btw, note that Object.keysand forEachare not available in ancient browsers, but you should use some polyfill anyway.

顺便说一句,请注意Object.keysforEach在古代浏览器中不可用,但无论如何您应该使用一些polyfill。