1

I am inserting an array of arrays inside constructor but when I console log it, I get a circular object array. I tried doing the same to a plain object but the same result came about. I tried looking online to understand this issue but the explanations make me more confused. Can anyone explain to me why the array "week" which consists of two separate arrays, becomes a circular reference when I put it into an object?

var week = []

localStorage.setItem('results', JSON.stringify(week));
/* console.log(JSON.parse(localStorage.getItem("results"))[0]) */

function Result (home, away, gh, ga) {
  this.home = home;
  this.away = away;
  this.gh = gh;
  this.ga = ga;
}


function seasons (id,inc,teamcount,collector ){

  this.id = id;
  this.inc = inc;
  this.teamcount = teamcount;
  this.collector = collector;
}

var Season1 = new seasons(0,0,4,[])


var stuff = {

id:1,
nu:14,
coll:[]


}


var game1 = new Result ("chelsea","barcelona",5,0) 
var game2 = new Result ("real madrid","liverpool",1,3) 

week.push(game1, game2)

var j = []

Season1.collector.push(week)
stuff.coll = week


console.log( week ,Season1, j, stuff)

console

[{
  away: "barcelona",
  ga: 0,
  gh: 5,
  home: "chelsea"
}, {
  away: "liverpool",
  ga: 3,
  gh: 1,
  home: "real madrid"
}], {
  collector: [[circular object Array]],
  id: 0,
  inc: 0,
  teamcount: 4
}, [], {
  coll: [circular object Array],
  id: 1,
  nu: 14
}

1 Answers1

0

Try separating the log first or try running Your code in different debugger:

console.log(week);
console.log(Season1);
console.log(j);
console.log(stuff);

As stated here:

The debugger is outputting [circular object Array] any time it's referring to an item already referred to.

Michał Moskal
  • 143
  • 2
  • 10