第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

扁平化一些嵌套數(shù)據(jù)時(shí)遇到問題

扁平化一些嵌套數(shù)據(jù)時(shí)遇到問題

繁星淼淼 2022-11-11 13:25:32
我有一些如下所示的數(shù)據(jù),我想獲得類型 student 的所有正面、負(fù)面和中性的總和,所以我做了一個 d3.nest 方法,其中我將鍵用作學(xué)生,并返回值的總和提及。{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 的結(jié)果,在 .rollup return 語句中我指定了這樣的值名稱等positive: d3.sum(d,function(f){ return f.positive;})。下面是結(jié)果。`key: "student"     value: {positive: 5, negative: 5, neutral: 0}`但是,要繪制雷達(dá)圖,我需要將數(shù)據(jù)展平,使其像下面這樣一個級別,但我不知道該怎么做。我試著像下面顯示的代碼那樣做,但一直出錯,請你幫忙。{student: student, positive:5, negative:5, neutral:0}我已經(jīng)嘗試過這個 forEach 循環(huán),但它沒有用var flatData = []            subStudent.forEach(function(sub){                sub.value(function(subval){                    flatData.push({                        level: sub.key,                        value: subval.value                    });                });            });            console.log(JSON.stringify(flatData))
查看完整描述

1 回答

?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個贊

可能不需要d3.nest(),除非你心里有理由?


你可以用 reduce 來做到這一點(diǎn)(但我也會包括d3.nest()下面的例子):


const input = [

  {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},

  {Type: 'other', positive: 2, negative: 0, neutral:1},

  {Type: 'other', positive: 1, negative: 1, neutral:0},

  {Type: 'other', positive: 1, negative: 1, neutral:0}

];


const output = Object.values(input.reduce((aggObj, item) => {

  

  if (!aggObj.hasOwnProperty(item.Type)) aggObj[item.Type] = item;

  else {

    for (let key in item){

      if (key != "Type") aggObj[item.Type][key] += item[key];

    } 

  }

  return aggObj

}, {}))


console.log(output)

輸入:


[

  { 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 },

  { Type: 'other',   positive: 2, negative: 0, neutral:1 },

  { Type: 'other',   positive: 1, negative: 1, neutral:0 },

  { Type: 'other',   positive: 1, negative: 1, neutral:0 }

]

輸出:


[

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

  { Type: "other",   positive: 4, negative: 2, neutral: 1 }

]

如果您需要/想要d3.nest()您可以這樣做(相同的輸入和輸出):


const input = [

  {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},

  {Type: 'other', positive: 2, negative: 0, neutral:1},

  {Type: 'other', positive: 1, negative: 1, neutral:0},

  {Type: 'other', positive: 1, negative: 1, neutral:0}

];


const nested = d3.nest()

  .key(d => d.Type)

  .rollup(d => ({

    positive: d3.sum(d, f => f.positive),

    negative: d3.sum(d, f => f.negative),

    neutral: d3.sum(d, f => f.neutral),

  }))  

  .entries(input)

  

const output = nested.map(item => {

  //console.log(item)

  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>


查看完整回答
反對 回復(fù) 2022-11-11
  • 1 回答
  • 0 關(guān)注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號