我正在try 将下面的JSON转换为excel,我使用了XLSX,它正在将我的JSON转换为excel,但dailyPointsArray的嵌套数组在转换为excel后为空.

Tried code

 const XLSX = require("xlsx");
 const workSheet = XLSX.utils.json_to_sheet(attendanceData);
    const workBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workBook, workSheet, "attendance");
    XLSX.write(workBook, { bookType: "xlsx", type: "buffer" });
    XLSX.write(workBook, { bookType: "xlsx", type: "binary" });
    XLSX.writeFile(workBook,"newExcel.xlsx");
attendanceData:[
  {
    workerId: '1230',
    workerFullName: 'A',
    workerDepartment: 'INFORMATION TECHNOLOGY',
    workerDesignation: 'ASSISTANT MANAGER',
    Location: 'locationA',
    dailyPointsArray: [
    {
      inTime: '-',
      Date: '23/03/2022',
      outTime: '-',
      Points: null,
      createdAs: 'ABSENT'
    },
    {
      inTime: '-',
      Date: '24/03/2022',
      outTime: '-',
      Points: null,
      createdAs: 'ABSENT'
    }
   ],
    total_duration: 0,
    total_shift_points: 0
  },
  {
    workerId: '1128',
    workerFullName: 'B',
    workerDepartment: 'INFORMATION TECHNOLOGY',
    workerDesignation: 'MANAGER',
    Location: 'LocationA',
    dailyPointsArray: [
    {
      inTime: '-',
      Date: '23/03/2022',
      outTime: '-',
      Points: null,
      createdAs: 'ABSENT'
    },
    {
      inTime: '-',
      Date: '24/03/2022',
      outTime: '-',
      Points: null,
      createdAs: 'ABSENT'
    }
   ],
    total_duration: 17,
    total_shift_points: 2
  },
]

Below is the excel file output enter image description here

As you can see the column of dailyPointsArray is empty. I want to my excel file should be like the below image enter image description here

推荐答案

try 展平数组:过滤嵌套数组,按所需顺序获取所需的键

试试这个:

const filtered = attendanceData.map(obj => {

    // get totals to add them later to keep column order (or use `header` param for columns order)
    const {
        dailyPointsArray,
        total_duration,
        total_shift_points,
        ...rest
    } = obj;

    // flatten..
    dailyPointsArray.map(el => {
        rest[el['Date']] = el.createdAs;
    });

    return {...rest,
        total_duration,
        total_shift_points
    };
});

const XLSX = require("xlsx");
const workSheet = XLSX.utils.json_to_sheet(filtered);
const workBook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workBook, workSheet, "attendance");
XLSX.write(workBook, { bookType: "xlsx", type: "buffer" });
XLSX.write(workBook, { bookType: "xlsx", type: "binary" });
XLSX.writeFile(workBook,"newExcel.xlsx");

Javascript相关问答推荐

如何将特定的字符串类型转换为TypScript中的字符串?

为什么这个自定义组件会被放置在TR之外?

输入有关HTML复选框的已判断属性的信息

获取表格的左滚动位置

使用useup时,React-Redux无法找到Redux上下文值

WebRTC关闭navigator. getUserMedia正确

google docs boldText直到按行执行应用脚本错误

我的角模板订阅后不刷新'

无法使用单击按钮时的useState将数据从一个页面传递到另一个页面

如何在Svelte中从一个codec函数中调用error()?

扩展类型的联合被解析为基类型

在Reaction中的handleSubmit按钮内,useSelector值仍然为空

JavaScript是否有多个`unfined`?

在FAQ Accodion应用程序中使用React useState时出现问题

是否可以将Select()和Sample()与Mongoose结合使用?

无法检索与Puppeteer的蒸汽游戏的Bundle 包价格

如何根据输入数量正确显示alert ?

React Refs不与高阶组件(HOC)中的动态生成组件一起工作

在高位图中显示每个y轴系列的多个值

设置复选框根据选中状态输入选中值