Yet, another question about JSON-like Object literals.
the object that I work with has the following structure:
let family ={
"id":"someId",
"participants":[
{
"name":"Q",
"sex":"m",
"age":23,
"occupations":[
{
"jobId":"some ID"
"finished": true,
"start": dateTime,
"end":dateTime,
"skills":[
{
"name":"welding"
},
{
"name":"concrete mixing"
},
]
},
{
"jobId": "someId",
"finished": false,
"skills":[
{
"name":"power lifting"
},
{
"name":"swimming"
},
{
}
]
},
{"OTHER Participant"},
{"OTHER Participant"}
]
}
It is for the sake of example.
when I receive data, every literal object is going to be unique so there is no "schema" that I can create and refer to based on the type, for example.
And I need to find the unique objects on the 2nd, 3rd, 4th etc levels.IF they exist
My take on this: Every time I would try to use something either like :
let personIneed; //to save him later;
let part = family.participants;
for (const xx of part){
let partJobs =xx.occupations;
for (const xxx of partJobs){
if(xxx.end && xxx.finished == true){
let partJobSkills = xxx.skills;
let isSkillPresent =false; //to change to true once skill iteration is finished
for (const xxxx of partJobSkills){
if(xxxx.name ==="concrete mixing"){
isSkillPresent =true;
}
}
//check if skill's there and save the participant
if(isSkillPresent){
personIneed = xx;
return;
}
}
}
}
Which is very bulky and if i have to build a function like that for every set of criteria... well I'd rather build a guillotine. It also looks a little bit like a callback Hell to me (the way it cascades and only) :)
Either when the search is only at the high level,
let part = family.participants;
let man = part.find (p=>p.sex==="m");
Which would only return me the first person to match the criteria. which is not ideal.
I would like to build a generalized function that will be able to go through the Object literals finding the nested objects that match my criteria. The criteria should be passed as parameters of the function. Function should also consider that there can be several nested objects with the same keys ( for example one person can be a Builder on many different jobs) or there can be in fact none of the objects that match the criteria. The ultimate goal is somehow to make all the nested objects easily accessible when the object is found. (for example when we found a person who has worked as a builder and who has finished his contract, we return this object but if i need to find the start and end of his working period, I need to go through all the nested objects again and it becomes hell again)
Please note that some arrays will simply not exist and some will have one value.