This may be a JS question, I'm not sure.
I am using JSON to store data about enemies in a game. I am currently structuring like this:
{
"Area 1": {
"Enemy Name": {
"type": "Human",
"lvl": 30
},
"Enemy 2": {
"type": "Human",
"lvl": 30
}
},
"Area 2": {
"Enemy 1": {
"type": "Human",
"lvl": 30
},
"Enemy 2": {
"type": "Human",
"lvl": 30
}
}
}
I was wondering if I should be instead using an array of enemies for each area with a "name"
property instead. Like this:
{
"Area 1": [
{
"name": "Enemy Name",
"type": "Human",
"lvl": 10
},
{
"name": "Enemy 2",
"type": "Human",
"lvl": 30
}
],
"Area 2": [
{
"name": "Enemy 1",
"type": "Human",
"lvl": 30
},
{
"name": "Enemy 2",
"type": "Human",
"lvl": 30
}
]
}
I believe the second is the way I should be doing it but wanted other's feedback. I think with the first way, I'll need to know the names of the enemies in order to read their data whereas, with the array, I can loop through the areas and read the enemies in each area. Thanks in advance for any help!
This may be a JS question, I'm not sure.
I am using JSON to store data about enemies in a game. I am currently structuring like this:
{
"Area 1": {
"Enemy Name": {
"type": "Human",
"lvl": 30
},
"Enemy 2": {
"type": "Human",
"lvl": 30
}
},
"Area 2": {
"Enemy 1": {
"type": "Human",
"lvl": 30
},
"Enemy 2": {
"type": "Human",
"lvl": 30
}
}
}
I was wondering if I should be instead using an array of enemies for each area with a "name"
property instead. Like this:
{
"Area 1": [
{
"name": "Enemy Name",
"type": "Human",
"lvl": 10
},
{
"name": "Enemy 2",
"type": "Human",
"lvl": 30
}
],
"Area 2": [
{
"name": "Enemy 1",
"type": "Human",
"lvl": 30
},
{
"name": "Enemy 2",
"type": "Human",
"lvl": 30
}
]
}
I believe the second is the way I should be doing it but wanted other's feedback. I think with the first way, I'll need to know the names of the enemies in order to read their data whereas, with the array, I can loop through the areas and read the enemies in each area. Thanks in advance for any help!
Share Improve this question edited Jun 22, 2017 at 5:14 Neil Lunn 151k36 gold badges355 silver badges325 bronze badges asked Jun 22, 2017 at 5:05 themadjemthemadjem 431 gold badge1 silver badge3 bronze badges 4- 1 I think with the second way you'll be able to more conveniently manipulate your data since it's in the form of array. Why is it an advantage? Because arrays are iterable by default, which is not the case with objects that are not iterable out of the box (but you can make them such). But remember that you won't be able to use functions, dates, and undefined in JSON arrays. – Ilya Kushlianski Commented Jun 22, 2017 at 5:13
- I agree with the "iterator" statement, but unless the question can actually be phrased in terms of it's "actual usage in your context" then it's really just opinion based. Though I actually believe named keys to be an "anti-pattern". IMO that is. – Neil Lunn Commented Jun 22, 2017 at 5:15
-
1
you could go one step beyond and create an array of area objects:
[{areaName:'Area 1',enemies:[{name:'enemy 1',type:'human'},{name:'enemy 2', type:'human'}]}, {}, {} ]
– Kaushal Niraula Commented Jun 22, 2017 at 5:19 - @KaushalNiraula This is what I just thought of as well. This would make for the easiest implementation and expandability. – themadjem Commented Jun 22, 2017 at 5:32
1 Answer
Reset to default 2The second option feels right. It is properly structured and you can easily loop through any Area to perform operations on Enemies. My suggestion is to go with second one.
var Map = [
Area1: [
{
name: "Enemy 1";
metaData: '...'
},
{
name: "Enemy 2";
metaData: '...'
}
],
Area2: [
{
name: "Enemy 1";
metaData: '...'
}
]
];
Scanning enemies in particular area.
Map[Area1].forEach(function(enemy) {
//Operation ...
});
I hope this helps. :)