I have a object obj1
which has mapping information which is present in other as values vals
. I am able to create flat object structure but recursive is not working so require help on this
const obj1 = [
{
mapping: "cities",
},
{
mapping: "category",
children: {
mapping: "type",
children: {
mapping: "otherType",
},
},
},
{
mapping: "age",
},
];
const vals = [
{
mapping: "category",
values: [{}],
},
{
mapping: "type",
values: [{}],
},
{
mapping: "otherType",
values: [{}],
},
{
mapping: "cities",
values: [{}],
},
{
mapping: "age",
values: [{}],
},
];
I want expected data in below fromat
const exectedData = {
cities: {
values: [{}],
},
category: {
values: [{}],
children: {
type: {
values: [{}],
children: {
otherType: {
values: [{}],
}
}
}
}
},
age: {
values: [{}]
}
}
2
Answers
You could transform your values to an object for faster update value finding and make a simple recursion:
Try this