0

I have the below code

var temp = [{ "a": "value", "b": "value" }, { "a": "value", "b": "value" }];

> console.log(temp) prints
> [Object,Object]

If I pull the same from database/api as below

var items = [];

data.forEach(function (item) {
    items.push({ "a": item.a , "b": item.b } );
})

> console.log(items) returns
> []

However if I expand the array, it has the same items.

How to push the elements so that the console.log prints the same as if in the first case.

enter image description here

Ben Thomas
  • 3,180
  • 2
  • 20
  • 38
Ningomba
  • 45
  • 5
  • 2
    This is almost certainly a combination of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) and [Is Chrome's JavaScript console lazy about evaluating arrays?](https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – JJJ May 30 '17 at 18:20
  • 1
    forEach is not async – Mihai Alexandru-Ionut May 30 '17 at 18:21
  • 1
    Of course not, but the part that the OP has left out almost certainly is ("I pull the same from database/api"). – JJJ May 30 '17 at 18:22
  • Thank you, the call was asyc and that created the issue. – Ningomba May 31 '17 at 08:28

0 Answers0