user12051965
user12051965

Reputation: 147

JavaScript get keys from object

I have the following object as you can see on console line 85. enter image description here

And I have the following JavaScript code

      for (var key in orderObject) {
        if (!orderObject.hasOwnProperty(key)) continue;
    
        var obj = orderObject[key];
        for (var prop in obj) {
            if (!obj.hasOwnProperty(prop)) 
              continue;
    
            console.log((prop + " = " + obj[prop]));
        }
      }

And you can see what this piece of code prints on the console on line 97.

What I want to achieve is first of all to access the keys, seller accepted the order , seller delivered the order etc. I can do for example Object.keys(orderObject) and it will print those keys. But I want to achieve so such manner that I get the key and the value for each item in that object. Ideally something like seller accepted the order, 1602709200 since I do not care about nanoseconds.

How can I achieve it?

I am mostly stuck because I treated it like an array an tried for(), forEach() but these cannot be used here.

Upvotes: 1

Views: 69

Answers (2)

niles87
niles87

Reputation: 71

You can try this for get the keys and values

For(const [key, value] of Object.entries(orderObject)){
   console.log(key,value)
}

Upvotes: 2

Bulent
Bulent

Reputation: 3411

for (const [key, value] of Object.entries(orderObject)) {
  console.log(`${key}: ${value.seconds}`);
}

Upvotes: 2

Related Questions