扁平化一些嵌套数据时遇到问题

我有一些如下所示的数据,我想获得类型 student 的所有正面、负面和中性的总和,所以我做了一个 d3.nest 方法,其中我将键用作学生,并返回值的总和提及。


{Type: student, positive: 2, negative: 1, neutral:0}, {Type: student, positive: 1, negative: 1, neutral:0}, {Type: student, positive: 1, negative: 1, neutral:0}, {Type: student, positive: 1, negative: 2, neutral:0} 

这是 d3.nest 的结果,在 .rollup return 语句中我指定了这样的值名称等positive: d3.sum(d,function(f){ return f.positive;})。下面是结果。


`key: "student"

     value: {positive: 5, negative: 5, neutral: 0}`

但是,要绘制雷达图,我需要将数据展平,使其像下面这样一个级别,但我不知道该怎么做。我试着像下面显示的代码那样做,但一直出错,请你帮忙。


{student: student, positive:5, negative:5, neutral:0}

我已经尝试过这个 forEach 循环,但它没有用


var flatData = []

            subStudent.forEach(function(sub){

                sub.value(function(subval){

                    flatData.push({

                        level: sub.key,

                        value: subval.value

                    });

                });

            });

            console.log(JSON.stringify(flatData))


繁星淼淼
浏览 134回答 1
1回答

Cats萌萌

可能不需要d3.nest(),除非你心里有理由?你可以用 reduce 来做到这一点(但我也会包括d3.nest()下面的例子):const input = [&nbsp; {Type: 'student', positive: 2, negative: 1, neutral:0},&nbsp; {Type: 'student', positive: 1, negative: 1, neutral:0},&nbsp; {Type: 'student', positive: 1, negative: 1, neutral:0},&nbsp; {Type: 'student', positive: 1, negative: 2, neutral:0},&nbsp; {Type: 'other', positive: 2, negative: 0, neutral:1},&nbsp; {Type: 'other', positive: 1, negative: 1, neutral:0},&nbsp; {Type: 'other', positive: 1, negative: 1, neutral:0}];const output = Object.values(input.reduce((aggObj, item) => {&nbsp;&nbsp;&nbsp; if (!aggObj.hasOwnProperty(item.Type)) aggObj[item.Type] = item;&nbsp; else {&nbsp; &nbsp; for (let key in item){&nbsp; &nbsp; &nbsp; if (key != "Type") aggObj[item.Type][key] += item[key];&nbsp; &nbsp; }&nbsp;&nbsp; }&nbsp; return aggObj}, {}))console.log(output)输入:[&nbsp; { Type: 'student', positive: 2, negative: 1, neutral:0 },&nbsp; { Type: 'student', positive: 1, negative: 1, neutral:0 },&nbsp; { Type: 'student', positive: 1, negative: 1, neutral:0 },&nbsp; { Type: 'student', positive: 1, negative: 2, neutral:0 },&nbsp; { Type: 'other',&nbsp; &nbsp;positive: 2, negative: 0, neutral:1 },&nbsp; { Type: 'other',&nbsp; &nbsp;positive: 1, negative: 1, neutral:0 },&nbsp; { Type: 'other',&nbsp; &nbsp;positive: 1, negative: 1, neutral:0 }]输出:[&nbsp; { Type: "student", positive: 5, negative: 5, neutral: 0 },&nbsp; { Type: "other",&nbsp; &nbsp;positive: 4, negative: 2, neutral: 1 }]如果您需要/想要d3.nest()您可以这样做(相同的输入和输出):const input = [&nbsp; {Type: 'student', positive: 2, negative: 1, neutral:0},&nbsp; {Type: 'student', positive: 1, negative: 1, neutral:0},&nbsp; {Type: 'student', positive: 1, negative: 1, neutral:0},&nbsp; {Type: 'student', positive: 1, negative: 2, neutral:0},&nbsp; {Type: 'other', positive: 2, negative: 0, neutral:1},&nbsp; {Type: 'other', positive: 1, negative: 1, neutral:0},&nbsp; {Type: 'other', positive: 1, negative: 1, neutral:0}];const nested = d3.nest()&nbsp; .key(d => d.Type)&nbsp; .rollup(d => ({&nbsp; &nbsp; positive: d3.sum(d, f => f.positive),&nbsp; &nbsp; negative: d3.sum(d, f => f.negative),&nbsp; &nbsp; neutral: d3.sum(d, f => f.neutral),&nbsp; }))&nbsp;&nbsp;&nbsp; .entries(input)&nbsp;&nbsp;const output = nested.map(item => {&nbsp; //console.log(item)&nbsp; return {Type: item.key, ...item.value}})console.log(output)<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript