Ray Saudlach
Ray Saudlach

Reputation: 650

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

Output of 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.

Upvotes: 2

Views: 5070

Answers (5)

Ele
Ele

Reputation: 33726

Use the Object.keys method

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

Upvotes: 1

keithlee96
keithlee96

Reputation: 1944

You are confusing yourself with the line 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

If you use the dot operator (.), then you reference it with the key name.

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

Upvotes: 0

user9306701
user9306701

Reputation:

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!

Upvotes: 1

RaphaMex
RaphaMex

Reputation: 2839

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.

Upvotes: 3

Sushanth --
Sushanth --

Reputation: 55740

You can use Object.keys and a foreach loop to access the properties on the object.

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

Object.keys(data).forEach(function(key) {
    console.log('key - ' + key + ' :: value - ' + data[key]);
});

Upvotes: 1

Related Questions