I want to refactor initial object in JavaScript to refactored object as below example.is there any way to do it with Lodash or in plain JavaScript?
const initialObject = {
status: 'success',
fields: [
{
name: 'price',
value: 12,
},
{
name: 'remain',
value: 45,
},
],
};
const RefactoredObject = {
status: 'success',
fields: [
{
price: 12,
},
{
remain: 45,
},
],
};
You can use map
here
const initialObject = {
status: 'success',
fields: [
{
name: 'price',
value: 12,
},
{
name: 'remain',
value: 45,
},
],
};
const result = {
...initialObject,
fields: initialObject.fields.map(({ name, value }) => ({ [name]: value })),
};
console.log(result);