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"
]
CodePudding user response:
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);
CodePudding user response:
You can use ...
to spread all entries of an Array:
const subjectResult = [];
for(var i = 0; i < result.length; i ){
subjectResult.push(...result[i].subject);
}
subjectResult.sort((a, b) => a.name < b.name? -1 : 1);
CodePudding user response:
I would prefer to create a new Set
from the list of subjects, to generate the unique list of subjects. Create a new Array from this Set
and sort it. This will give the unique list of sorted subjects.
Working Fiddle
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 subjects = Array.from(new Set(result.flatMap(res => res.subject.map(sub => sub.name)))).sort();
console.log(subjects);
CodePudding user response:
You can't get "name" from the array "subject"
This should work:
let subjectResult = [];
for(var _result of result){
_result.subject.map((subject) => {
subjectResult.push(subject.name);
})
}
console.log(subjectResult);
Full 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": []
}
]
}
]
let subjectResult = [];
for(var _result of result){
_result.subject.map((subject) => {
subjectResult.push(subject.name);
})
}
console.log(subjectResult);
CodePudding user response:
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 final = result.flatMap((items) => items.subject.reduce((acc, cur) => {
acc.push(cur.name)
return acc;
}, [])) .sort()
console.log(final);