0

I've a simple question for learning javascript.

I create an array with objects like this

var myresult =new Array(); [];
for (i= 2015;i<=2030;i=i+1)
{
      var newobject={'myname' : i};
      myresult.push(newobject);
}
console.log ('Result:'+myresult);
console.log(JSON.stringify(myresult));

In Console I see this output

Result:[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

[{"myname":2015},{"myname":2016},{"myname":2017},{"myname":2018},{"myname":2019},{"myname":2020},{"myname":2021},{"myname":2022},{"myname":2023},{"myname":2024},{"myname":2025},{"myname":2026},{"myname":2027},{"myname":2028},{"myname":2029},{"myname":2I30}]

Why cant I see it in this way? enter image description here

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
RainerS
  • 423
  • 1
  • 4
  • 11

1 Answers1

1

You used string concatenation, which stringifies the object as JSON. Use

console.log('Result:', myresult);

instead.

EDIT: I just tried this in Firefox Javascript console:

>> const myresult = [];
undefined

>> const newobject = {'myname': 1};
undefined

>> console.log('NEWOBJECT:', newobject);
NEWOBJECT: Object { myname: 1 }
debugger eval code:1:1
undefined

>> myresult.push(newobject);
1

>> console.log('MYRESULT:', myresult);
MYRESULT: Array [ {…} ]
debugger eval code:1:1
undefined

i.e. logging the object works fine.

Stefan Becker
  • 5,695
  • 9
  • 20
  • 30
  • may i do something wrong by creating the array and objects? I want to have a output of my array with object like the image in my post ... – RainerS Jan 19 '19 at 10:18
  • Have you tried `console.log('NEWOBJECT:', newobject)`? – Stefan Becker Jan 19 '19 at 10:21
  • thats all? The output is now like in the picture, so i think my way to define array and objects is correctly - thanks a lot @Stefan Becker – RainerS Jan 19 '19 at 10:52