I have an array of Javascript objects that look like these:
data = [{PK: "Country1", Prop1: "true", children:[
{PK: "State1", Prop1: "true", children:[
{PK: "City1", Prop1: "Value1"}]
}]
},
{PK: "Country1", Prop2: "true", children:[
{PK: "State2", Prop2: "true", children:[
{PK: "City2", Prop2: "Value2"}]
}]
},
{PK: "Country1", Prop3: "true", children:[
{PK: "State2", Prop3: "true", children:[
{PK: "City3", Prop3: "Value3"}]
}]
}]
and I am trying to merge them based on the PK property. Think of them as countries, states, and cities. Currently each object has a country, under its children property is a state and under it is a city. I want them to be merged such that if both objects have the same country, their states will be merged and if both states are the same, their cities will added together in the children property. Then, if a city has a property Prop1, the state should indicate it also has that property, hence the Prop1 = "true". This also follows on the country level. To make it clearer, I am trying to make it look like this:
data = [{PK: "Country1", Prop1: "true", Prop2: "true", Prop3: "true" children:[
{PK: "State1", Prop1: "true", children:[
{PK: "City1", Prop2: "Value1"}]
},
{PK: "State2", Prop2: "true", Prop3: "true", children:[
{PK: "City2", Prop2: "Value2"},
{PK: "City3", Prop3: "Value3"}]
}]
}]
I tried something like this but I can't wrap it around the children object which is also an array of objects. Can someone help me on this or lead me to a better answer. Thanks!