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.

N8888
  • 670
  • 2
  • 14
  • 20
Ray Saudlach
  • 530
  • 1
  • 6
  • 19

5 Answers5

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.

RaphaMex
  • 2,781
  • 1
  • 14
  • 30
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]);
});
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • Correct, this would work, as other ways of doing so. I suggest you specify which *properties of the object* are accessed by this method. – RaphaMex Feb 06 '18 at 01:03
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!

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

Ele
  • 33,468
  • 7
  • 37
  • 75
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"]

keithlee96
  • 1,724
  • 2
  • 11
  • 17