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

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

向分組數(shù)據(jù)添加缺失值

向分組數(shù)據(jù)添加缺失值

炎炎設(shè)計 2022-08-04 16:09:07
這是我的問題的后續(xù)。我從對象數(shù)組中按日期獲取分組值。當我對值進行分組時,如果按日期對每天缺少的類型進行分組,則可以將指標填寫為 0。這是我的數(shù)組: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"        }    ]以下是我對數(shù)據(jù)進行分組的方式:const groupBy = (array, key) => {    return array.reduce((result, currentValue) => {      (result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);      return result;    }, {});};const personGroupedByColor = groupBy(arr, 'date');我的結(jié)果是: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條經(jīng)驗 獲得超6個贊

您可以創(chuàng)建所有不同值的 a,然后循環(huán)訪問 中的每個值,檢查它們是否具有所有不同的值,如果沒有,則推送具有該類型和度量的新對象: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);


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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