猿问

向分组数据添加缺失值

这是我的问题的后续。我从对象数组中按日期获取分组值。当我对值进行分组时,如果按日期对每天缺少的类型进行分组,则可以将指标填写为 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?


炎炎设计
浏览 127回答 1
1回答

慕哥9229398

您可以创建所有不同值的 a,然后循环访问 中的每个值,检查它们是否具有所有不同的值,如果没有,则推送具有该类型和度量的新对象:SettypepersonGroupedByColortype0arr = [{    "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);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答