Javascript Objects format conversion

Tags: ,



How can i convert this:

{key: "A", value: {count:2}}
{key: "B", value: {count:5}}
{key: "C", value: {count:1}}

INTO THIS:

{key:"A", value: 2}
{key:"B", value: 5}
{key:"C", value: 1}

Answer

You could use map

const data = [
  { key: "A", value: { count: 2 } },
  { key: "B", value: { count: 5 } },
  { key: "C", value: { count: 1 } },
]

const res = data.map((obj) => ({
  key: obj.key,
  value: obj.value.count,
}))

console.log(res)


Source: stackoverflow