为了账号安全,请及时绑定邮箱和手机立即绑定

向分组数据添加缺失值

向分组数据添加缺失值

炎炎设计 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 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号