简体   繁体   English

访问Javascript对象键

[英]Accessing Javascript Object Keys

I'm having the hardest figuring out how to this (seems so simple). 我正在最努力地弄清楚该怎么做(似乎很简单)。

I have a Javascript Object as shown here 我有一个Javascript对象,如下所示

Output of console.log(data): console.log(data)的输出:

{"prevExists":false,"pubKey":"b5","ID":"5f1"}

I'm trying to access the different key value pairs. 我正在尝试访问不同的键值对。

When I try the expected methods, I get back undefined. 当我尝试预期的方法时,我将返回未定义状态。

I have tried: 我努力了:

var pubKey = "pubKey";
data.pubKey
data[pubkey];
data["pubKey"];

I know I'm missing something really obvious here. 我知道我在这里确实缺少一些明显的东西。

You can use Object.keys and a foreach loop to access the properties on the object. 您可以使用Object.keysforeach循环来访问对象的属性。

 var data = {"prevExists":false,"key":"b5","ID":"5f1"}; Object.keys(data).forEach(function(key) { console.log('key - ' + key + ' :: value - ' + data[key]); }); 

First you need to create a reference to your object. 首先,您需要创建对对象的引用。 Like this: 像这样:

var myObj = { "prevExists": false, "key": "b5", "ID": "5f1" };

Then, you can access the elements using their keys: 然后,您可以使用其键访问元素:

console.log(myObj["prevExists"]);

Console exit: 控制台出口:

false

Good luck! 祝好运!

Use the Object.keys method 使用Object.keys方法

 var data = {"prevExists":false,"pubKey":"b5","ID":"5f1"} console.log(Object.keys(data)); 

Object.keys()

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well). Object.keys()方法返回给定对象自己的可枚举属性的数组,其顺序与for...in循环所提供的顺序相同(区别在于for-in循环枚举原型链中的属性为:好)。

You have several ways of accessing keys, depending on which keys you're talking about. 您有几种访问键的方法,具体取决于您所谈论的键。

In your example, any of those would work: 在您的示例中,任何一种都可以工作:

var data = {
    "prevExists":false,
    "pubKey":"b5",
    "ID":"5f1"
};

// Access all keys of enumerable string-keyed properties
Object.keys(data).forEach((key) => console.log(key,data[key]));
// Access all keys of enumerable and non-enumerable string-keyed properties
Object.getOwnPropertyNames(data).forEach((key) => console.log(key,data[key]));
// Access all keys of enumerable string-keyed properties of your object, its prototype, and all the prototype chain...
for (let key in data)
    console.log(key,data[key]);

If you want to have a better understanding of what is an object's property, you can have a look at this recent answer I wrote on the topic. 如果您想更好地了解什么是对象的属性,可以看看我在主题上写的这个最新答案

You are confusing yourself with the line var pubKey="pubKey". 您将自己与var pubKey =“ pubKey”行混淆了。 There are 2 ways to access object parameters: 有两种访问对象参数的方法:

const data = {"prevExists":false,"pubKey":"b5","ID":"5f1"};
// var pubKey = "pubKey"; This line is not needed

1) data.pubKey 1)data.pubKey

If you use the dot operator (.), then you reference it with the key name. 如果使用点运算符(。),则将其与键名一起引用。

2) data["pubKey"]; 2)data [“ pubKey”];

If you use brackets ([]), then you use the string that matches the key. 如果使用方括号([]),则使用与键匹配的字符串。

If you add the line: 如果添加行:

const pubKey = "pubKey";

, then data[pubKey] will also work, because it evaluates to data["pubKey"] ,那么data [pubKey]也将起作用,因为它的计算结果为data [“ pubKey”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM