0

I have an object list in JavaScript, I want to get and parse "total" in a variable. But whenever I print or filter in console log, it always give null value.

var dd = result1[0] // orignal data
console.log(typeof dd) // this is object
console.log(dd); // this printing whole object list

// try 1
console.log(JSON.stringify(dd));

// try 2
for (const key in dd) {
if (dd.hasOwnProperty(key)) {
    console.log(`${key}: ${dd[key]}`);
    }
}

enter image description here

I tried JSON.stringify(dd), but nothing worked.

rensothearin
  • 670
  • 1
  • 5
  • 24
Sycra
  • 1
  • how does `result1[0]` looks like – brk Mar 28 '23 at 07:09
  • What is result1. – kennarddh Mar 28 '23 at 07:10
  • console.log(dd.total); // this should get the total value according to image description – Arman Mar 28 '23 at 07:12
  • 1
    There is probably some asynchronous code involved, calculating that `total` and you are not awaiting it properly. `console.log` can be deceiving – derpirscher Mar 28 '23 at 07:15
  • @derpirscher doesn't even have to be asynchronous. The typical example of this behaviour is `const obj = { foo: 1 }; console.log(obj, obj.foo); obj.foo = 2;` where the code is fully synchronous but the value you would see after expanding the object in the console is `2` not `1`. – VLAZ Mar 28 '23 at 07:26
  • @VLAZ yes, you are right. But the typical case when one expects a variable or property to have a value at some point in the code, but it doesn't, is, that it's calculated asynchronously and not awaited properly – derpirscher Mar 28 '23 at 07:32
  • I have already tried console.log(dd.total); , but ERROR total not found. – Sycra Mar 28 '23 at 08:02
  • result1 also have same structure just add 2 value, so I used 0 index to get first. – Sycra Mar 28 '23 at 08:07

0 Answers0