You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular基于PDFMake处理嵌套JSON生成动态表格并提取wind10m属性列

实现方案

1. 直接访问嵌套属性(简单场景适用)

你只需要在构建表格行数据时,直接通过层级访问语法读取嵌套字段即可,核心代码示例如下:

// 1. 定义表头,新增风向、风速两个列
const tableHeaders = [
  '时间点', '云量', '视宁度', '透明度', '抬升指数', '2米相对湿度',
  '风向', '风速', '2米温度', '降水类型'
];

// 2. 遍历数据组装行
const tableBody = [tableHeaders];
yourData.dataseries.forEach(record => {
  tableBody.push([
    record.timepoint,
    record.cloudcover,
    record.seeing,
    record.transparency,
    record.lifted_index,
    record.rh2m,
    // 直接取嵌套属性,可选链可避免wind10m为空时报错
    record.wind10m?.direction ?? '-',
    record.wind10m?.speed ?? '-',
    record.temp2m,
    record.prec_type
  ]);
});

// 3. 传给pdfmake配置
const pdfConfig = {
  content: [{ table: { body: tableBody } }]
};

2. 通用动态嵌套字段读取(适用字段可配置场景)

如果你原本是通过字段配置数组动态生成列,不需要硬编码行数据,可以写一个通用的嵌套属性读取工具函数:

// 嵌套属性取值工具函数
function getValueByPath(obj, path) {
  return path.split('.').reduce((res, key) => res?.[key], obj) ?? '-';
}

// 字段配置数组,支持嵌套路径写法
const fieldPaths = [
  'timepoint', 'cloudcover', 'seeing', 'transparency',
  'lifted_index', 'rh2m', 'wind10m.direction', 'wind10m.speed',
  'temp2m', 'prec_type'
];

// 动态生成行
const tableBody = [tableHeaders];
yourData.dataseries.forEach(record => {
  const row = fieldPaths.map(path => getValueByPath(record, path));
  tableBody.push(row);
});

内容的提问来源于stack exchange,提问作者Dabrownie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 13:36:04