In Image 1, the output you will see if you log the final
array from Snippet 1.
My goal is to transform my array to match the format shown in Image 2.
I attempted using lodash's _.uniqBy()
method [Snippet 2], but the logged output of the reduce
variable still resembles Image 1. How can I modify my final
array to resemble the desired output in Image 2?
https://i.sstatic.net/TiYHw.png Image 1
https://i.sstatic.net/rZ8WA.png
Image 2
const reduce = final.reduce((x, y) => {
if (x.department !== y.department) {
return x;
}
})
console.log(final)
console.log(reduce);
Snippet 1
_.uniqBy(final, 'department')
Snippet 2
UPDATE
This scenario pertains to TypeScript (.ts
) rather than JavaScript (.js
).
Data Structure =>
[{ department: string, professors: string[] }]
Regarding the request
The source of my data is specified here
[
{
department: 'Department of Mathematics - INTRAMUROS',
professors: [
'Sabino, Lilibeth D.',
'Department Chair',
.....
Below is an excerpt of my code before arriving at the final
array:
const recommend = [];
const final = [];
recommended.forEach((prof) => {
const find = proffessorDetails.find((collection) => {
return collection.professors.find((professor) => professor == prof);
});
recommend.push(find);
})
const find = recommend.map((collection) => {
return {
department: collection.department,
prof: _.intersection(collection.professors, recommended)
};
});
final.push(find);