向分组数据中添加缺失值

时间:2020-05-07 02:46:39

标签: javascript jquery arrays json ecmascript-6

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

答案 0 :(得分:2)

您可以为所有不同的Set值创建一个type,然后遍历personGroupedByColor中的每个值,检查它们是否具有所有不同的type值,如果没有,则推入具有该类型和度量标准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;
  }, {});
};

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);