1

I have the following data structure:

const data = {
    school1:{location:'medway',score:6},
    school2:{location:'milan',score:8},
    school3:{location:'brooklyn',score:103,
    }

How can I convert it to the following format in javascript:

const data = [
    {name:'school1', location:'medway',score:6},
    {name:'school2', location:'milan',score:8},
    {name:'school3', location:'brooklyn',score:103}
    ]
Olly
  • 157
  • 6

1 Answers1

1

You can map over the entries of the object.

const data={school1:{location:"medway",score:6},school2:{location:"milan",score:8},school3:{location:"brooklyn",score:103}};
const res = Object.entries(data).map(([name, v]) => ({name, ...v}));
console.log(res);
Unmitigated
  • 76,500
  • 11
  • 62
  • 80