You can use Array#map function on the object keys and create your objects with desired shape.
const total = {
'Apple': 0.6,
'Banana': 0.6,
'Orange': 1,
'Grapes': 0.4,
'Pineapple': 0.4
};
const array = Object.keys(total)
.map(key => ({ name: key, value: total[key] }))
.sort((f, s) => f.value - s.value);
console.log(array);
If you use ES7 or higher you can replace Object#keys with Object#entries. Use also object destructuring in the parameter list to get name
and value
separately.
const total = {
'Apple': 0.6,
'Banana': 0.6,
'Orange': 1,
'Grapes': 0.4,
'Pineapple': 0.4
};
const array = Object.entries(total)
.map(([name, value]) => ({ name, value }))
.sort((f, s) => f.value - s.value);;
console.log(array);