javascript - How to JSON.stringify an array of objects -


i attempting json.stringify() following key/value pair, value array of objects.

var string = json.stringify({onlineusers : getusersinroom(users, room)}); 

this incorrect , gives following error:

var string = json.stringify({onlineusers : getusersinroom(users, room)});

                ^ 

typeerror: converting circular structure json

this method:

function getusersinroom(users, room) {     var json = [];     (var = 0; < users.length; i++) {         if (users[i].room === room) {              json.push(users[i]);         }     }     return json; } 

added users data structure:

[  {      id:1,      username:"",      room:"room 1",      client: {          sessionid:1,          key:value      }  },  {      // etc  } ] 

added function add user users array.

function adduser(client) {     clients.push(client);     var = clients.indexof(client);     if (i > -1) {         users.push({             id : i,             username : "",             room : "",             client : clients[i]         });     } } 

added screen capture of javascript array containing object key/value pairs inside object.

enter image description here

added screen capture of clients array containing websocket objects. enter image description here

how correctly "stringify" {key: arrayofobjects[{key:value,key:{}},{},{}]}?

var data = { }; data.onlineusers = getusersinroom();  var string = json.stringify(data); 

would work you?

edit

i noticed error circular type, user or room object creating circular reference.

user > room > user > room etc...


Comments