如何用空值预填充数组?

时间:2019-08-13 20:59:27

标签: javascript arrays multidimensional-array

我正在尝试将从API接收到的某些数据填充到图表中,并且为了使其“正常工作”,我需要确保数组中的每个对象都具有{{1} } 12属性下的值(一年中的每个月一个值)。

我从sum收到的数据如下:

API

因此,如您所见,[ { "sum": { "201901": 8, "201902": 16, "201903": 8, "201904": 8, /* This project is missing data for month 5 and 6 */ "201907": 5, "201908": 2, "201909": 0, "201910": 2, "201911": 5, "201912": 8 }, "project": "Project Name 1" }, { "sum": { "201901": 9, "201902": 18, "201903": 9, "201904": 9, "201905": 17, "201906": 5, "201907": 2, "201908": 4, /* This project is missing data for month 9, 10, 11, 12 */ }, "project": "Project Name 2" }, { "sum": { "201901": 2, "201902": 2, "201903": 5, "201904": 2, "201905": 0, "201906": 10, "201907": 45, "201908": 21, "201909": 3, "201910": 2, "201911": 17, "201912": 16 }, "project": "Project Name 3" } ] project 1缺失了几个月,但是我仍然需要用空值填充它们(例如:project 2,{{ 1}}等),因为我的图表中的"201905": 0始终是"201906": 0值,如果X-axis没有给出12值,就会搞砸了。 所以我的问题是:如何填充每个对象以始终为一年中的每个月提供一个值?

到目前为止,我正在这样遍历数据:

Y-axis

有效地给了我一些帮助:

12

3 个答案:

答案 0 :(得分:1)

您可以取一个空的sum对象并将值分配给一个新对象。

var data = [{ sum: { 201901: 8, 201902: 16, 201903: 8, 201904: 8, 201907: 5, 201908: 2, 201909: 0, 201910: 2, 201911: 5, 201912: 8 }, project: "Project Name 1" }, { sum: { 201901: 9, 201902: 18, 201903: 9, 201904: 9, 201905: 17, 201906: 5, 201907: 2, 201908: 4 }, project: "Project Name 2" }, { sum: { 201901: 2, 201902: 2, 201903: 5, 201904: 2, 201905: 0, 201906: 10, 201907: 45, 201908: 21, 201909: 3, 201910: 2, 201911: 17, 201912: 16 }, project: "Project Name 3" }], empty = { 201901: 0, 201902: 0, 201903: 0, 201904: 0, 201905: 0, 201906: 0, 201907: 0, 201908: 0, 201909: 0, 201910: 0, 201911: 0, 201912: 0 },
    empty = { 201901: 0, 201902: 0, 201903: 0, 201904: 0, 201905: 0, 201906: 0, 201907: 0, 201908: 0, 201909: 0, 201910: 0, 201911: 0, 201912: 0 },
    result = data.map(({ sum, ...rest }) => ({ sum: { ...empty, ...sum }, ...rest }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

如果您定义一个包含所有12 keys的对象,例如:

const normalizedSum = {
  "201901": 0,
  "201902": 0,
  "201903": 0,
  "201904": 0,
  "201905": 0,
  "201906": 0,
  "201907": 0,
  "201908": 0,
  "201909": 0,
  "201910": 0,
  "201911": 0,
  "201912": 0
};

然后,您可以像这样在input数组上使用Array.map()

input.map(({sum, project}) => ({sum: {...normalizedSum, ...sum}, project}));

完整示例:

const input = [{"sum":{"201901":8,"201902":16,"201903":8,"201904":8,"201907":5,"201908":2,"201909":0,"201910":2,"201911":5,"201912":8},"project":"Project Name 1"},{"sum":{"201901":9,"201902":18,"201903":9,"201904":9,"201905":17,"201906":5,"201907":2,"201908":4,},"project":"Project Name 2"},{"sum":{"201901":2,"201902":2,"201903":5,"201904":2,"201905":0,"201906":10,"201907":45,"201908":21,"201909":3,"201910":2,"201911":17,"201912":16},"project":"Project Name 3"}];

const normalizedSum = {
  "201901": 0,
  "201902": 0,
  "201903": 0,
  "201904": 0,
  "201905": 0,
  "201906": 0,
  "201907": 0,
  "201908": 0,
  "201909": 0,
  "201910": 0,
  "201911": 0,
  "201912": 0
};

let res = input.map(
  ({sum, project}) => ({sum: {...normalizedSum, ...sum}, project})
);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

答案 2 :(得分:1)

您可以做的是制作一个模板对象,该对象具有全部12个月的默认值0。然后浏览sum项,如果它们具有匹配的日期键,则将其设置为sum中键的值:

for (let key in chartData) {
  // template object holding 0s
  let data = { "201901": 0, "201902": 0, "201903": 0, "201904": 0, "201905": 0, "201906": 0, "201907": 0, "201908": 0, "201909": 0, "201910": 0, "201911": 0, "201912": 0 };
  // For each key that exist in the sum object populate data with that value
  for (let month in chartData[key].sum) data[month] = chartData[key].sum[month];

  emptyArr.push({
    name: chartData[key].project,
    data: Object.values(data), // Now you can do the same thing you where doing before
  });
}

这是一个完整的例子:

let chartData = [
  {
    "sum": {
      "201901": 8,
      "201902": 16,
      "201903": 8,
      "201904": 8,
      /* This project is missing data for month 5 and 6 */
      "201907": 5,
      "201908": 2,
      "201909": 0,
      "201910": 2,
      "201911": 5,
      "201912": 8
    },
    "project": "Project Name 1"
  },
  {
    "sum": {
      "201901": 9,
      "201902": 18,
      "201903": 9,
      "201904": 9,
      "201905": 17,
      "201906": 5,
      "201907": 2,
      "201908": 4,
      /* This project is missing data for month 9, 10, 11, 12 */
    },
    "project": "Project Name 2"
  },
  {
    "sum": {
      "201901": 2,
      "201902": 2,
      "201903": 5,
      "201904": 2,
      "201905": 0,
      "201906": 10,
      "201907": 45,
      "201908": 21,
      "201909": 3,
      "201910": 2,
      "201911": 17,
      "201912": 16
    },
    "project": "Project Name 3"
  }
];

let emptyArr = []

for (let key in chartData) {
	let data = { "201901": 0, "201902": 0, "201903": 0, "201904": 0, "201905": 0, "201906": 0, "201907": 0, "201908": 0, "201909": 0, "201910": 0, "201911": 0, "201912": 0 };
	for (let month in chartData[key].sum) data[month] = chartData[key].sum[month];
  	
  emptyArr.push({
    name: chartData[key].project,
    data: Object.values(data),
  });
}

console.log(emptyArr);