从给定对象中获取Array-Object-String [英] get Array-Object-String thing from the given Object
本文介绍了从给定对象中获取Array-Object-String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的对象 freq = {a:50,r:25,m:25}
我想将其转换为 Array-Object 之类的东西
I have such an Object
freq = { a: 50, r: 25, m: 25 }
I want to convert it to this Array-Object like thing
dps = [
{ label: "a",
y: 50 },
{ label: "r",
y: 25 },
{ label: "m",
y: 25 }
];
这是用于创建带有画布的图表.
This is for creating a Chart with canvas.
推荐答案
您可以获取对象的条目并获取简写属性.
You could take the entries of the object and take a destructuring assignment for the key/value pairs and map new objects with short hand properties.
var freq = { a: 50, r: 25, m: 25 },
dps = Object.entries(freq).map(([label, y]) => ({ label, y }));
console.log(dps);
这篇关于从给定对象中获取Array-Object-String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文