0

I'm a JavaScript beginner and I'm sure there is simple explanation for this but I have can't find it. After two levels of list nesting, why am I getting [Object] and not the nested lists themselves? Thanks.

var obj1 = {v: 1, rest: null}    
var obj2 = {v: 2, rest: obj1}
var obj3 = {v: 3, rest: obj2}    
var obj4 = {v: 4, rest: obj3}    
var obj5 = {v: 5, rest: obj4}

{ v: 1, rest: null }

{ v: 2, rest: { v: 1, rest: null } }

{ v: 3, rest: { v: 2, rest: { v: 1, rest: null } } }

{ v: 4, rest: { v: 3, rest: { v: 2, rest: [Object] } } }

{ v: 5, rest: { v: 4, rest: { v: 3, rest: [Object] } } }

yuriy636
  • 11,171
  • 5
  • 37
  • 42
PeteG
  • 9
  • 2
  • 7
    You are probably displaying that in the browser console, aren't you? The data is still there, its just displayed as [Object], i'm assuming just for readability purposes. :) – vicbyte Jan 27 '18 at 21:11
  • where do you get the result? – Nina Scholz Jan 27 '18 at 21:11
  • You're using an object `{}` and not an array `[]`. What's the result you're trying to achieve? – Tamir Kfir Jan 27 '18 at 21:13
  • 1
    Are you `console.log()`'ing your output? Then it would not be suprising. Your terminal is only saving space. Probably you are scripting server-side, aren't you? – Michael W. Czechowski Jan 27 '18 at 21:18
  • yes this is all server side and yes, console.logging the output. Found the answer in the linked duplicate, using util: console.log(util.inspect(obj5, false, null)) Thanks everyone. – PeteG Jan 28 '18 at 15:38

0 Answers0