I have multiple arrays. Now I’m trying to merge all the arrays, but I’m not getting the result I expect.
I only need to retrieve all subjects in an alphabetical list.
This is my code:-
result = [
{
"id": 1,
"name": "Pre Clinical",
"subject": [
{
"id": 1,
"name": "Human Physiology",
"topic": [
{
"id": 1,
"name": "Topic 1"
},
{
"id": 2,
"name": "Topic 2"
},
{
"id": 3,
"name": "Topic 3"
},
{
"id": 4,
"name": "Topic 4"
}
]
},
{
"id": 15,
"name": "Anatomy",
"topic": []
},
{
"id": 16,
"name": "Biochemistry",
"topic": []
}
]
},
{
"id": 2,
"name": "Para Clinical",
"subject": [
{
"id": 2,
"name": "Pathology",
"topic": [
{
"id": 5,
"name": "Topic 5"
},
{
"id": 6,
"name": "Topic 6"
},
{
"id": 7,
"name": "Topic 7"
},
{
"id": 8,
"name": "Topic 8"
},
{
"id": 9,
"name": "Topic 9"
}
]
},
{
"id": 9,
"name": "Forensic Medicine & Toxicology",
"topic": []
},
{
"id": 17,
"name": "Microbiology",
"topic": []
}
]
},
{
"id": 3,
"name": "Clinical",
"subject": [
{
"id": 3,
"name": "Ophthalmology",
"topic": [
{
"id": 10,
"name": "Topic 10"
}
]
},
{
"id": 4,
"name": "Preventive and Social Medicine",
"topic": []
},
{
"id": 5,
"name": "Radiology",
"topic": []
},
{
"id": 6,
"name": "ENT",
"topic": []
},
{
"id": 7,
"name": "Medicine",
"topic": []
},
{
"id": 11,
"name": "Community Medicine",
"topic": []
},
{
"id": 12,
"name": "Psychiatry",
"topic": []
},
{
"id": 18,
"name": "Anaesthesiology",
"topic": []
},
{
"id": 21,
"name": "Otorhinolaryngology",
"topic": []
},
{
"id": 24,
"name": "Orthopaedics",
"topic": []
},
{
"id": 25,
"name": "Paediatrics",
"topic": []
},
{
"id": 26,
"name": "Dermatology & Venereology",
"topic": []
},
{
"id": 27,
"name": "Obstetrics & Gynaecology",
"topic": []
},
{
"id": 28,
"name": "Pharmacology",
"topic": []
},
{
"id": 29,
"name": "Surgery Essence",
"topic": []
}
]
}
]
const subjectResult = [];
for(var i = 0; i < result.length; i++){
subjectResult.push(result[i].subject.name);
}
console.log(subjectResult);
This returns:
[
undefined,
undefined,
undefined
]
…but I would like to get:
[
"Anaesthesiology",
"Anatomy",
"Biochemistry",
"Community Medicine",
"Dermatology & Venereology",
"ENT",
"Forensic Medicine & Toxicology",
"Human Physiology",
"Medicine",
"Microbiology",
"Obstetrics & Gynaecology",
"Ophthalmology",
"Orthopaedics",
"Otorhinolaryngology",
"Paediatrics",
"Pathology",
"Pharmacology",
"Preventive and Social Medicine",
"Psychiatry",
"Radiology",
"Surgery Essence"
]
>Solution :
You can use flatMap:
const subjectResult = result.flatMap(({subject}) => subject.map(({name}) => name));
And if you need it to be sorted, chain a sort() call to it:
const result = [{"id": 1,"name": "Pre Clinical","subject": [{"id": 1,"name": "Human Physiology","topic": [{"id": 1,"name": "Topic 1"},{"id": 2,"name": "Topic 2"},{"id": 3,"name": "Topic 3"},{"id": 4,"name": "Topic 4"}]},{"id": 15,"name": "Anatomy","topic": []},{"id": 16,"name": "Biochemistry","topic": []}]},{"id": 2,"name": "Para Clinical","subject": [{"id": 2,"name": "Pathology","topic": [{"id": 5,"name": "Topic 5"},{"id": 6,"name": "Topic 6"},{"id": 7,"name": "Topic 7"},{"id": 8,"name": "Topic 8"},{"id": 9,"name": "Topic 9"}]},{"id": 9,"name": "Forensic Medicine & Toxicology","topic": []},{"id": 17,"name": "Microbiology","topic": []}]},{"id": 3,"name": "Clinical","subject": [{"id": 3,"name": "Ophthalmology","topic": [{"id": 10,"name": "Topic 10"}]},{"id": 4,"name": "Preventive and Social Medicine","topic": []},{"id": 5,"name": "Radiology","topic": []},{"id": 6,"name": "ENT","topic": []},{"id": 7,"name": "Medicine","topic": []},{"id": 11,"name": "Community Medicine","topic": []},{"id": 12,"name": "Psychiatry","topic": []},{"id": 18,"name": "Anaesthesiology","topic": []},{"id": 21,"name": "Otorhinolaryngology","topic": []},{"id": 24,"name": "Orthopaedics","topic": []},{"id": 25,"name": "Paediatrics","topic": []},{"id": 26,"name": "Dermatology & Venereology","topic": []},{"id": 27,"name": "Obstetrics & Gynaecology","topic": []},{"id": 28,"name": "Pharmacology","topic": []},{"id": 29,"name": "Surgery Essence","topic": []}]}];
const subjectResult = result.flatMap(({subject}) => subject.map(({name}) => name))
.sort();
console.log(subjectResult);