Following is my array, and I need to replace the keys name
with title
and Email
with subtitle
.
I tried some ways, but I still need to fulfill my requirement. Please provide any solution to this.
const newUpdatedList = [];
resArr.forEach((res) => {
const obj = {
title: res.name,
subtitle: res.attributes.Email
};
if (res.children) {
const newList = res.children.map((ch) => {
return {
title: ch.name,
subtitle: ch.attributes.Email,
};
});
obj.children = newList;
}
newUpdatedList.push(obj);
});
const resArr =
[ { user_id : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
, name : 'Harsha ABC'
, custom_id : 'mani78989-1gfqv04bo'
, attributes : { Email: 'harsha@gmail.com', Role: 'admin'}
, children:
[ { user_id : 'd748037a-b445-41c2-b82f-4d6ee9396714'
, name : 'Lavaraju Allu'
, custom_id : 'mani78989-1gfqv472q'
, attributes : { Email: 'raju@gmail.com', Role: 'Manager'}
, children:
[ { user_id : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
, name : 'Ramesh Allu'
, custom_id : 'mani78989-1gh14i13t'
, attributes : { Email: 'ramesh@gmail.com', Role: 'Retailer'}
, children:
[ { user_id : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
, name : 'Harsha ABC'
, custom_id : 'mani78989-1gh15nrev'
, attributes : { Email: 'harsha@gmail.com', Role: 'Delivery Manager'}
, children : []
} ] } ] }
, { user_id : '550cc296-d7e4-44fb-9d62-4c6755b3f6f2'
, name : 'Suresh Kunisetti'
, custom_id : 'mani78989-1gfqv6idi'
, attributes : { Email: 'suresh@gmail.com', Role: 'Super Admin'}
, children:
[ { user_id : '45cf19f8-36c1-4669-9333-1226c4f7b66b'
, name : 'Harish Three'
, custom_id : 'mani78989-1ggv5vffb'
, attributes : { Email: 'harish234@gmail.com', Role: 'Delivery Manager'}
, children : []
} ] }
, { user_id : '2c8535be-5fe7-40f0-892f-0f9bcffe0baa'
, name : 'Sandeep Bbb'
, custom_id : 'mani78989-1gh14m5p4'
, attributes : { Email: 'sandeep@gmail.com', Role: 'Delivery Manager'}
, children : []
}
, { user_id : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
, name : 'Ramesh Allu'
, custom_id : 'mani78989-1gh14pc6p'
, attributes : { Email: 'ramesh@gmail.com', Role: 'Manager'}
, children : [ ]
} ] }
]
Expected output is
const resArr =
[ { user_id : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
, title : 'Harsha ABC'
, custom_id : 'mani78989-1gfqv04bo'
, attributes : { subtitle: 'harsha@gmail.com', Role: 'admin'}
, children:
[ { user_id : 'd748037a-b445-41c2-b82f-4d6ee9396714'
, title : 'Lavaraju Allu'
, custom_id : 'mani78989-1gfqv472q'
, attributes : { subtitle: 'raju@gmail.com', Role: 'Manager'}
, children:
[ { user_id : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
, title : 'Ramesh Allu'
, custom_id : 'mani78989-1gh14i13t'
, attributes : { subtitle: 'ramesh@gmail.com', Role: 'Retailer'}
, children:
[ { user_id : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
, title : 'Harsha ABC'
, custom_id : 'mani78989-1gh15nrev'
, attributes : { subtitle: 'harsha@gmail.com', Role: 'Delivery Manager'}
, children : []
} ] } ] }
, { user_id : '550cc296-d7e4-44fb-9d62-4c6755b3f6f2'
, title : 'Suresh Kunisetti'
, custom_id : 'mani78989-1gfqv6idi'
, attributes : { subtitle: 'suresh@gmail.com', Role: 'Super Admin'}
, children:
[ { user_id : '45cf19f8-36c1-4669-9333-1226c4f7b66b'
, title : 'Harish Three'
, custom_id : 'mani78989-1ggv5vffb'
, attributes : { subtitle: 'harish234@gmail.com', Role: 'Delivery Manager'}
, children : []
} ] }
, { user_id : '2c8535be-5fe7-40f0-892f-0f9bcffe0baa'
, title : 'Sandeep Bbb'
, custom_id : 'mani78989-1gh14m5p4'
, attributes : { subtitle: 'sandeep@gmail.com', Role: 'Delivery Manager'}
, children : []
}
, { user_id : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
, title : 'Ramesh Allu'
, custom_id : 'mani78989-1gh14pc6p'
, attributes : { subtitle: 'ramesh@gmail.com', Role: 'Manager'}
, children : []
} ] }
]