What is the best way to turn
{"john":15, "alex":19, "mark":19, "luke":12}
into
const data = [
{ name: "john", value: 15},
{ name: "alex", value: 19},
{ name: "mark", value: 19},
{ name: "luke", value: 12},
];
Thanks any help is appreciated
CodePudding user response:
can solve using map
on the object entries
const a = {"john":15, "alex":19, "mark":19, "luke":12}
const res = Object.entries(a).map(([name,value]) => ({name,value}))
console.log(res)
CodePudding user response:
I think the best way is using Object.entries
& reduce
:
const obj = { john: 15, alex: 19, mark: 19, luke: 12 };
const array = Object.entries(obj).reduce((acc, curr) => {
const [ name, value ] = [curr[0],curr[1]];
return acc.concat({ name, value });
}, []);
console.log(array);