There are many suggestions out there recommending looping through the object to find what you need, but I'm not convinced that this is the best approach in my situation.
Currently, I have an array with entries
linking to another array called people
(containing id
and name
) using person_id
, as well as another array called projects
(also containing id
and name
) using project_id
.
What I require is the ability to access both the project and person with a specific id
within the loop on entries
, so I can retrieve their names. Following the suggestions of others would involve looping through both the people
and projects
arrays for each iteration of entries
, which seems inefficient.
Therefore, I came up with the idea of creating what I call a "hashtable" from both the people
and projects
arrays during initialization. This involves creating new objects called people_hashtable
and projects_hashtable
, where the key is the id.
For example:
[
{
"id": "8",
"name": "John Doe"
}
]
would be transformed into:
{
"8": {
"name": "John Doe"
}
}
This way, I can easily access the name without constantly looping through the arrays while still retaining the original order of the array (which is why I am not outputting it directly from the server like this; you can't quite order an object). I also use both people
and projects
in a selectbox that needs to be ordered by name.
Am I on the right track with this approach? Is there a better way to handle this? Or should I abandon this idea altogether and stick with the search loop suggested in other questions?
I aim to be as efficient as possible on both the server and client sides.