I have the below sample array of objects
let arr = [{id: 1,name: 'Test',segment: 'test',subCategory: [{id: 1,name:'Test1',segment:'test1'}]}]
I need to format this array to the below structure
let newArr = [{link: 'test', name: 'Test',subCategory: [{link: 'test1',name: 'Test1'}]}]
How to do this in an easy step in typescript?
>Solution :
You can simply use Map method to achieve your desired result:
let arr = [
{ id: 1, name: 'Test', segment: 'test', subCategory: [{ id: 1, name:
'Test1', segment: 'test1' }] }
];
let newArr = arr.map(item => {
return {
link: item.segment,
name: item.name,
subCategory: item.subCategory.map(subItem => {
return {
link: subItem.segment,
name: subItem.name
};
})
};
});
console.log(newArr);