JimNayzium
Coder
When I am structuring my JSON data objects at the beginning of the process, I find myself wanting the JSON to have an actual ID it can be indexed from. I come from a simple PHP background ages ago where associative arrays were how record sets were always returned from the database.
Is there an understood best way to do this? I am starting to learn Firebase, which is exclusively just a huge JSON Datafile/Database, and they warn that it is best to just use the flat arrays for larger data sets as opposed to coding our own index values.
So here are two simplified JSON objects that illustrate my point, followed by the way to retrieve some targeted data.
Can anyone give me the best practices method for structuring this data?
And if there is not always a best way to do this, I welcome your actual opinions on the matter as well.
I find myself always leaning toward what I call "the altObject" in the example, but I fear this will cause me regrets the farther my journey takes me. I thought I would ask now up front before I created a terrible headache for myself months from now.
Which way is best? Better? Thoughts?
Is there an understood best way to do this? I am starting to learn Firebase, which is exclusively just a huge JSON Datafile/Database, and they warn that it is best to just use the flat arrays for larger data sets as opposed to coding our own index values.
So here are two simplified JSON objects that illustrate my point, followed by the way to retrieve some targeted data.
Can anyone give me the best practices method for structuring this data?
And if there is not always a best way to do this, I welcome your actual opinions on the matter as well.
I find myself always leaning toward what I call "the altObject" in the example, but I fear this will cause me regrets the farther my journey takes me. I thought I would ask now up front before I created a terrible headache for myself months from now.
JavaScript:
let regularObject = {
"otherStuff": { "cool": "stuff" },
"data": {
"week": [
{
"week": 1,
"infoArray": [
{
"id": 1,
"cool": "stuff"
},
{
"id": 2,
"cool": "Other stuff"
}
]
},
{
"week": 2,
"infoArray": [
{
"id": 3,
"cool": "stuff but different"
},
{
"id": 4,
"cool": "Other cool stuff but different still"
}
]
}
]
}
};
let altObject = {
"otherStuff": { "cool": "stuff" },
"data": {
"week": {
"1": {
"infoArray": [
{
"id": 2222222,
"cool": "stuff"
},
{
"id": 33333333,
"cool": "Other stuff"
}
]
},
"2": {
"infoArray": [
{
"id": 444444444,
"cool": "stuff but different"
},
{
"id": 555555555,
"cool": "Other cool stuff but different still"
}
]
}
}
}
};
let weekVar = 1;
let seemsEasierToUse = altObject.data.week[weekVar].infoArray.map((item) => {
return item;
});
console.log(seemsEasierToUse);
let seemsHarderToUse = regularObject.data.week.find(week => week.week === weekVar).infoArray.map((item) => {
return item;
});
console.log(seemsHarderToUse);
Which way is best? Better? Thoughts?
