2

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.

3

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Correct, this would work, as other ways of doing so. I suggest you specify which properties of the object are accessed by this method.
1

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!

2 Comments

I suggest you change myArray to data, because it's definitely not an array ;)
Oh, sure! Thank you!
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.