【发布时间】:2021-02-25 21:17:05
【问题描述】:
我有一个看起来像这样的数组。
0: Object { age: 1.22128, initials: "YY", major: "Internet Content & Information" }
1: Object { age: 0.623491, initials: "RAM", major: "Banks Diversified" }
2: Object { age: 3.47226, initials: "W", major: "Internet Retail" }
3: Object { age: 0.10981, initials: "DAM", major: "Communication Equipment" }
4: Object { age: 0, symbol: initials", major: "Software Application" }
5: Object { age: 0.82384, initials: "SAM", major: "Internet Content & Information" }
6: Object { age: 1.62574, initials: "LISA", major: "Internet Content & Information" }
7: Object { age: 0, initials: "AN", college: "major Content & Information" }
8: Object { age: 1.34786, initials: "PAM", major: "REIT Retail" }
9: Object { age: 0, initials: "DREW", major: "Software—Infrastructure" }
我想做的是把专业分类成一个图表,我可以使用这段代码来做到这一点。下面。
var data = array,
result = _(data)
.groupBy('major')
.map((group, major) =>
({ industry, children: _.map(group, ({ initials: major, age }) => ({ major, age })) }))
.value(),
// final = [{ name: "Children Array", children: result }];
final = result
我用来将首字母分配给专业的图表采用这样的值。
const data = [
{
type: 'Retail',
value: 38,
},
{
type: 'Software',
value: 5,
},
{
type: 'Engineering',
value: 35,
},
{
type: 'Real Estate',
value: 15,
},
{
type: 'HealthCare',
value: 7,
},
]
使用上面的代码摘录,我能够将它放在一个看起来像这样的数组中。
(18) […]
0: Object { major: "Internet Content & Information", children: (5) […] }
1: Object { major: "Banks Diversified", children: (1) […] }
2: Object { major: "Internet Retail", children: (1) […] }
3: Object { major: "Communication Equipment", children: (2) […] }
4: Object { major: "Software Application", children: (3) […] }
孩子们指出该专业中有多少个首字母,有没有办法可以创建一个新数组来模仿图表在代码中的方式,这样我就可以将专业作为类型,将孩子的值作为值在图表中。
如果有人可以帮助我将顶部的数组转换为图表的数据语法,我也会很高兴。 谢谢你
【问题讨论】:
标签: javascript node.js arrays json object