求一个js方法实现表头与表体数据结合成key:value形式?

现在有一份这样得数据:

{
    "code": 0,
    "msg": null,
    "data": {
        "columns": [{
            "columnCode": "ID",
            "columnType": "STRING"
        }, {
            "columnCode": "DATE",
            "columnType": "DATE"
        }, {
            "columnCode": "OIL",
            "columnType": "NUMERIC"
        }, {
            "columnCode": "GAS",
            "columnType": "NUMERIC"
        }],
        "rows": [
            ["1", "2022-08-08 14:25:05", 23.123, 12.123],
            ["2", "2022-08-09 14:25:24", 14.123, 45.123],
            ["3", "2022-08-10 14:25:44", 15.123, 16.123],
            ["4", "2022-08-11 14:25:05", 12.123, 16.123],
            ["5", "2022-08-12 14:25:05", 15.123, 24.123],
            ["6", "2022-08-13 14:25:05", 16.123, 23.123]
        ]
    }
}

希望通过一个方法组织成这样一个数据:
可以看到得规律是:columns是表头,rows是表体。但是表体与表头对应关系为表体得下标。表头得对应字段是columnCode;
怎么实现?

[
{"ID": "1", "DATE": "2022-08-08 14:25:05", "OIL":23.123, "GAS": 12.123 },
{"ID": "2", "DATE": "2022-08-09 14:25:24", "OIL":14.123, "GAS": 45.123 },
...
]

以此类推。求一个方法实现这个功能!

阅读 2.3k
5 个回答

js 代码

const resp = {...};

function work({data: {columns: cols, rows}}) {
    return rows.map(row => Object.fromEntries(
        cols.map((col, i) => [col.columnCode, row[i]])
    ));
}

console.log(work(resp));

结果

[
    {"ID": "1", "DATE": "2022-08-08 14:25:05", "OIL": 23.123, "GAS": 12.123},
    {"ID": "2", "DATE": "2022-08-09 14:25:24", "OIL": 14.123, "GAS": 45.123},
    {"ID": "3", "DATE": "2022-08-10 14:25:44", "OIL": 15.123, "GAS": 16.123},
    {"ID": "4", "DATE": "2022-08-11 14:25:05", "OIL": 12.123, "GAS": 16.123},
    {"ID": "5", "DATE": "2022-08-12 14:25:05", "OIL": 15.123, "GAS": 24.123},
    {"ID": "6", "DATE": "2022-08-13 14:25:05", "OIL": 16.123, "GAS": 23.123}
]

一种实现方式:

const req = {
    "code": 0,
    "msg": null,
    "data": {
        "columns": [
            {
                "columnCode": "ID",
                "columnType": "STRING"
            },
            {
                "columnCode": "DATE",
                "columnType": "DATE"
            },
            {
                "columnCode": "OIL",
                "columnType": "NUMERIC"
            },
            {
                "columnCode": "GAS",
                "columnType": "NUMERIC"
            }
        ],
        "rows": [
            ["1", "2022-08-08 14:25:05", 23.123, 12.123],
            ["2", "2022-08-09 14:25:24", 14.123, 45.123],
            ["3", "2022-08-10 14:25:44", 15.123, 16.123],
            ["4", "2022-08-11 14:25:05", 12.123, 16.123],
            ["5", "2022-08-12 14:25:05", 15.123, 24.123],
            ["6", "2022-08-13 14:25:05", 16.123, 23.123]
        ]
    }
};

const filterMap = (req) => {
    const { data } = req;
    const { rows } = data;
    const cols = data.columns.map(({ columnCode }) => columnCode);
    return rows.map((row) => {
        const res = {};
        cols.forEach((col, i) => (res[col] = row[i]));
        return res;
    });
};

console.log(filterMap(req));

const {columns,rows} = data;
const result = rows.map(items => columns.reduce((r,{columnCode}, index) => Object.assign(r,{[columnCode]:items[index]}),{}));

const data = {
  columns: [
    {
      columnCode: "ID",
      columnType: "STRING"
    },
    {
      columnCode: "DATE",
      columnType: "DATE"
    },
    {
      columnCode: "OIL",
      columnType: "NUMERIC"
    },
    {
      columnCode: "GAS",
      columnType: "NUMERIC"
    }
  ],
  rows: [
    ["1", "2022-08-08 14:25:05", 23.123, 12.123],
    ["2", "2022-08-09 14:25:24", 14.123, 45.123],
    ["3", "2022-08-10 14:25:44", 15.123, 16.123],
    ["4", "2022-08-11 14:25:05", 12.123, 16.123],
    ["5", "2022-08-12 14:25:05", 15.123, 24.123],
    ["6", "2022-08-13 14:25:05", 16.123, 23.123]
  ]
};

function fomartData(arr) {
  const { columns, rows } = data;
  const tableData = rows.map((item) => {
    const obj = {};
    item.forEach((line, i) => {
      const { columnCode } = columns[i];
      obj[columnCode] = line;
    });
    return obj;
  });
  return tableData;
}

fomartData(data)

还是挺简单的:

function deal(columns, rows) {
  return rows.map(item => {
    const obj = {};
    item.forEach((x, index) => (obj[columns[index].columnCode] = x));
    return obj;
  });
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题