'How to properly change a array of object to another array of object in TypeScript [closed]
How to properly change a array of object to another array of object in TypeScript?
a : [
{bcdbcd : 1, abcabc : "value1"},
{bcdbcd : 2, abcabc : "value2"},
{bcdbcd : 3, abcabc : "value3"}
]
a : [
{label: 1, value: "value1"},
{label: 2, value: "value2"},
{label: 3, value: "value3"}
]
I want to change the code from top to bottom.
Solution 1:[1]
You can directly use the input object properties to frame your output.
a = [
{bcdbcd : 1, abcabc : "value1"},
{bcdbcd : 2, abcabc : "value2"},
{bcdbcd : 3, abcabc : "value3"}
];
const response = a.map(({bcdbcd, abcabc}) => ({label: bcdbcd, value: abcabc}));
console.log(response)
I have used object destruction functionality and prepared the response object like you mentioned.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mchowdam |