reduce使用

111 阅读1分钟
//* 原数组处理为table可用数据
let resultTable = bugMarkList.reduce((preArr, item) => {
  let findObj = preArr.find(
    i => String(i.bugTypeName) === String(item.bugTypeName)
  );
  if (!findObj) {
    const option = {
      bugTypeName: item.bugTypeName,
      backgroundColor: item.backgroundColor,
      bugStatus: 0,
      type: "成虫",
      num: String(item.bugStatus) === "0" ? 1 : 0
    };
    const option1 = {
      bugTypeName: item.bugTypeName,
      backgroundColor: item.backgroundColor,
      bugStatus: 1,
      type: "幼虫",
      num: String(item.bugStatus) === "1" ? 1 : 0
    };
    preArr.push(option);
    preArr.push(option1);
  } else {
    preArr.find(i => {
      if (
        String(i.bugTypeName) === String(item.bugTypeName) &&
        String(i.bugStatus) === String(item.bugStatus)
      ) {
        i.num += 1;
      }
    });
  }
  return preArr;
}, []);
this.QggcQData.tableData = resultTable;