亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

向分組數據添加缺失值

向分組數據添加缺失值

炎炎設計 2022-08-04 16:09:07
這是我的問題的后續。我從對象數組中按日期獲取分組值。當我對值進行分組時,如果按日期對每天缺少的類型進行分組,則可以將指標填寫為 0。這是我的數組:arr = [        {           "date": "2020-01-01",           "metric": 32,           "type": "Google"        },        {           "date": "2020-01-01",           "metric": 24,           "type": "Bing"        },        {           "date": "2020-01-02",           "metric": 1,           "type": "Google"        },        {           "date": "2020-01-02",           "metric": 32,           "type": "Jeeves"        },        {           "date": "2020-01-03",           "metric": 24,           "type": "Bing"        },        {           "date": "2020-01-03",           "metric": 30,           "type": "Google"        }    ]以下是我對數據進行分組的方式:const groupBy = (array, key) => {    return array.reduce((result, currentValue) => {      (result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);      return result;    }, {});};const personGroupedByColor = groupBy(arr, 'date');我的結果是:2020-01-01: 0: {date: "2020-01-01", metric: 32, type: "Google"}1: {date: "2020-01-01", metric: 24, type: "Bing"}2020-01-02: 0: {date: "2020-01-02", metric: 1, type: "Google"}1: {date: "2020-01-02", metric: 32, type: "Jeeves"}2020-01-03: 0: {date: "2020-01-03", metric: 24, type: "Bing"}1: {date: "2020-01-03", metric: 30, type: "Google"}有什么辦法可以得到:2020-01-01: 0: {date: "2020-01-01", metric: 32, type: "Google"}1: {date: "2020-01-01", metric: 24, type: "Bing"}2: {date: "2020-01-01", metric: 0, type: "Jeeves"}2020-01-02: 0: {date: "2020-01-02", metric: 1, type: "Google"}1: {date: "2020-01-02", metric: 0, type: "Bing"}2: {date: "2020-01-02", metric: 32, type: "Jeeves"}2020-01-03: 0: {date: "2020-01-03", metric: 30, type: "Google"}1: {date: "2020-01-03", metric: 24, type: "Bing"}2: {date: "2020-01-03", metric: 0, type: "Jeeves"}是否可以將缺失值替換為指標 0?
查看完整描述

1 回答

?
慕哥9229398

TA貢獻1877條經驗 獲得超6個贊

您可以創建所有不同值的 a,然后循環訪問 中的每個值,檢查它們是否具有所有不同的值,如果沒有,則推送具有該類型和度量的新對象:SettypepersonGroupedByColortype0


arr = [{

    "date": "2020-01-01",

    "metric": 32,

    "type": "Google"

  },

  {

    "date": "2020-01-01",

    "metric": 24,

    "type": "Bing"

  },

  {

    "date": "2020-01-02",

    "metric": 1,

    "type": "Google"

  },

  {

    "date": "2020-01-02",

    "metric": 32,

    "type": "Jeeves"

  },

  {

    "date": "2020-01-03",

    "metric": 24,

    "type": "Bing"

  },

  {

    "date": "2020-01-03",

    "metric": 30,

    "type": "Google"

  }

]


const groupBy = (array, key) => {

  return array.reduce((result, currentValue) => {

    (result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);

    return result;

  }, {});

};


let personGroupedByColor = groupBy(arr, 'date');


const types = new Set(arr.map(a => a.type));


for (a in personGroupedByColor) {

  types.forEach(t => {

    if (!personGroupedByColor[a].some(v => v.type == t)) {

      personGroupedByColor[a].push({

        "date": personGroupedByColor[a][0].date,

        "metric": 0,

        "type": t

      });

    }

  })

}

console.log(personGroupedByColor);


查看完整回答
反對 回復 2022-08-04
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號