I have an object of objects of a company's hierarchy selection, for example:
{
1: { division: "division1" },
2: { division: "division2" },
11: { division: "division2", branch: "branch2" },
41: { division: "division2", branch: "branch2", department: "department2" },
100: { division: "division2", branch: "branch2", department: "department10" },
102: { division: "division2", branch: "branch3" },
130: { division: "division17" },
144: { division: "division17" , branch: "branch22" },
200: { division: "division50" }
}
And I need to get the unique objects but most detailed ones. So the above example should result in:
{
1: { division: "division1" },
41: { division: "division2", branch: "branch2", department: "department2" },
100: { division: "division2", branch: "branch2", department: "department10" },
102: { division: "division2", branch: "branch3" },
144: { division: "division17" , branch: "branch22" },
200: { division: "division50" }
}
These are selections from 3 dropdowns, each selection holds the data of the entire hierarchy up to that item (The hierarchy is division > branch > department). So if a user selects division2 then branch2, it will add both { division: "division2" } and { division: "division2", branch: "branch2" } but I only need the most detailed one, { division: "division2", branch: "branch2" } in this case.
*Edit: It is not necessary to keep the original keys
How can I do that?
division1, but added now. I hope that clarifies things now. But I will add more info. However you already understood because you corrected me aboutdivision1