RainerS
RainerS

Reputation: 521

Output of object in console failed

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

Upvotes: 0

Views: 41

Answers (1)

Stefan Becker
Stefan Becker

Reputation: 5952

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.

Upvotes: 1

Related Questions