1

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.

user12051965
  • 97
  • 9
  • 29
  • Does this answer your question? [How do I loop through or enumerate a JavaScript object?](https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object) – Daniel_Knights Oct 17 '20 at 19:52

2 Answers2

2
for (const [key, value] of Object.entries(orderObject)) {
  console.log(`${key}: ${value.seconds}`);
}
Bulent
  • 3,307
  • 1
  • 14
  • 22
2

You can try this for get the keys and values

For(const [key, value] of Object.entries(orderObject)){
   console.log(key,value)
}
niles87
  • 71
  • 1
  • 2
  • 6