I have a very basic programing question that i hoped you can shed a light on.
I am working a lot of objects right now and i was wondering if it is better to search for content inside an array of objects or inside a nested object?
For eg, i can store the same data sample in the following two ways:
data1 = [
{ "id":1, "key1: "value1", "key2:"value2"},
{ "id":2, "key1: "value1", "key2:"value2"},
{ "id":3, "key1: "value1", "key2:"value2"},
{ "id":4, "key1: "value1", "key2:"value2"},
.....
]
and
data2 = {
"id_1": { "key1: "value1", "key2:"value2"},
"id_2": { "key1: "value1", "key2:"value2"},
"id_3": { "key1: "value1", "key2:"value2"},
"id_4": { "key1: "value1", "key2:"value2"},
.....
}
Now the requirement is to get a certain property from a child object. And all we know is the id (and not the index) associated with it.
If i were to use the array method, i will have to use loops and array filters to access any content/value in the individual objects. This method seems rather cumbersome to be and iterating through each child object feels very inefficient to me. Yet whenever i see similar data samples being implemented by experienced programmers, they all seem to using arrays a lot.
If I were to use the nested object method, all i have to do is call data2.id_2.key2
to get that specific value.
Which is the recommended way of doing things? I will be playing with rather large datasets and so, option will have better performance?