5

Sorry for my last question being so confusing, I was confused my self, but now I got a proper example:

var obj = {};
obj.entities = [];
obj.entities["player"] = [];
obj.entities["player"]["0"] = [];
obj.entities["player"]["0"]["pos"] = "0,0";

var jsonStr = JSON.stringify(jsonObj);

// {"entities":[]}
console.log(JSON.stringify(obj));

The output of JSON.stringify(obj) is wrong as you can see. What causes this ?

0

6 Answers 6

12

You're first building an array ([]), then assigning properties to it with non-number keys (player). This is technically possible (as in not causing an error), but it's not what arrays are for.

You should use objects ({}) instead. Also, ["player"] is the same as .player.

var obj = {};
obj.entities = {};
obj.entities.player = []; // array here because you're filling with indices ([0])
obj.entities.player[0] = {}; // object again, because non-indices as keys (`pos`)
obj.entities.player[0].pos = "0,0";

Objects can take any property key. Arrays are a subset of objects, which should only have indices (numbers >= 0) as keys.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah, thanks for the explanation, I wasn't fully aware of the object-array difference. But I could still do obj.entities.player = {}; and then obj.entities.player.pos = "0,0"; couldn't I ?
@elias94xx: If you have one player, then that would be possible. Though the ["0"] you wrote makes me assume you have a list (array) of players somehow.
Yeah sorry, I forgot about the id for a second. :)
3

Your life would be much easier if you'd define your objects in JSON to begin with:

var obj = {
  'entities': [
    {'player':{'pos': '0,0'}}
  ]
};

Comments

1

You're using named array indeces instead of object key/value pairs.

var obj = {};
obj.entities = {};
obj.entities["player"] = {};
obj.entities["player"]["0"] = [];
obj.entities["player"]["pos"] = "0,0";

// {"entities":{"player":{"0":[],"pos":"0,0"}}}
console.log(JSON.stringify(obj));

Comments

1

entities, and entities["player"] and entities["player"]["0"] need to be objects, not arrays.

You're adding properties to these arrays, rather than pushing values onto them. These custom properties are not getting stringified.

The fix is simple:

var obj = {};
obj.entities = {};   //  <------------ this is an object now
obj.entities["player"] = {};    // <--------- also an object
obj.entities["player"]["0"] = {};   // <-------- and so on
obj.entities["player"]["0"]["pos"] = "0,0";

Comments

1

I think you have some serious confusions about arrays and objects in javascript. An array ([]) works only with positive integer indexes. In your example you are doing the following:

obj.entities = [];
obj.entities["player"] = [];

You say that obj.entities is an array and then you use player as index. And player is not an integer. So this code makes no sense. On the other hand you could use objects with properties. Those properties can be strings:

obj.entities = {};
obj.entities.player = [];
obj.entities.player[0] = 'foo bar';

Comments

1

You are confusing objects with arrays. The following code will work.

var obj = {};
obj.entities = {};
obj.entities.player = [];
obj.entities.player[0] = {};
obj.entities.player[0].pos = "0,0";

The things you went wrong:

  1. An array can have only integer indexes. So it's incorrect writing a["1"] if you intend to use a as an array.
  2. To be correctly serialized, only an object can have named properties, like object.entities.player or object.entities["player"].

1 Comment

Note that keys are always strings, so there is no difference between ["1"] or [1]. The last is merely more semantical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.