Skip to content
Advertisement

Javascript Objects format conversion

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}

Advertisement

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)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement