I am working on some code I am unfamiliar with.
I have a data set that looks like this:
[
{"id": 1, "person_name": "joe", "parent_id": "1ax"}
{"id": 2, "person_name": "jane", "parent_id": "5t6"}
{"id": 3, "person_name": "john", "parent_id": "a88"}
]
Originally the code has this line:
const values = this.data.map((d) => d.person_name).sort();
and then this:
groups = Object.values(
values.reduce((a, c) => {
(a[c] || (a[c] = { name: c, rcount: 0 })).rcount += 1;
return a;
}, {})
).sort(({ rcount: ac }, { rcount: bc }) => bc - ac);
This all results in something like this:
[
{name: "joe", rcount: 1},
{name: "jane", rcount: 1},
{name: "john", rcount: 1}
]
I'd like to include the parent_id in the final output too but can't figure out how.
I've tried a number of things in the .map() section but none have yet worked.
I tried this for example:
const values = this.data.map((d) => d.person_name, d.parent_id).sort();
Is there a way I can map to an object and then reference the person_name and the parent_id from values when doing the reduce?
person_namein the list. If you addparent_id, you may get different values if aperson_nameexists twice in the list (in which case it would have shown up as{ name: "name", rcount: 2 }), but with differentparent_ids (in which case it may show up as{ name: "name", parent_id: "1ax", rcount: 1 }, { name: "name", parent_id: 5t6, rcount: 1 })...