2

我有一个对象数组(arr),其中每个对象看起来像这样:-

{
   "name" : "someName",
   "address" : "someAddress",
   "attributes" : [
      {
        "attName" : "Sea",
      },
      {
        "attName" : "Beach",
      }
    ],
   "values" : [ 
      "2540",
      "3345",
      "2340"
    ]
}

数组“值”和“属性”对于不同的对象可能有不同数量的元素。

现在,当我使用以下代码将此对象数组转换为 csv 时:

const json2csv = require('json2csv').parse;
const csvString = json2csv(arr);

我得到以下输出:

name, address, attributes, values
"someName","someAddress", [{"attName" : "Sea"},{"attName" : "Beach"}], ["2540", "3345", "2340"]

但是,我希望我的 csv 是这样的:

name, address, attName, attName, value, value, value
"someName","someAddress", "Sea", "Beach", "2540", "3345", "2340"

我知道 JSON 不允许多个具有相同名称的键,但我不知道如何实现这一点。

4

1 回答 1

1

将数组列扩展为具有相同标题的多个列是非标准的。

但是,如果您真的想这样做,您可以编写一个自定义函数来将对象数组转换为二维字符串数组。然后使用库将二维字符串数组转换为 csv。

例如:

// import a library to convert 2d string array into csv string
let { to_csv } = require("@beenotung/tslib/csv");

// the sample object array
let records = [{
        "name": "someName",
        "address": "someAddress",
        "attributes": [
            {
                "attName": "Sea",
            },
            {
                "attName": "Beach",
            }
        ],
        "values": [
            "2540",
            "3345",
            "2340"
        ]
    }];

let csv = object_array_to_csv(records)
console.log(csv);
/* will print out below lines:
name,address,attName,attName,values,values,values
someName,someAddress,Sea,Beach,2540,3345,2340
*/

// convert an object array into csv text
function object_array_to_csv(objects) {
    let records = objects.map(object_to_opt_list);
    let titles = records.find(record => record.length > 0)
        .map(field => field.key);
    let values = records.map(record => record.map(field => field.value));
    let rows = [titles, ...values];
    let csv = to_csv(rows);
    return csv;
}

// convert an object with string and array of string/object into 2d key-value array
function object_to_opt_list(object) {
    let opt_list = [];
    for (const [key, value] of Object.entries(object)) {
        if (Array.isArray(value)) {
            opt_list.push(...value_to_opt_list(key, value));
        }
        else {
            opt_list.push({ key, value });
        }
    }
    return opt_list;
}

// convert recursively expand the array/object value into 2d key-value array
function value_to_opt_list(key, value) {
    if (Array.isArray(value)) {
        return value.map(value => value_to_opt_list(key, value))
            .reduce((acc, c) => acc.concat(c));
    }
    if (typeof value === "object") {
        return object_to_opt_list(value);
    }
    return [{ key, value }];
}
于 2020-08-06T07:42:27.057 回答