1 回答
TA贡献1865条经验 获得超7个赞
将数组列扩展为多个同名列是非标准的。
但是,如果你真的想这样做,你可以编写一个自定义函数来将对象数组转换为二维字符串数组。然后使用库将二维字符串数组转换为 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 }];
}
添加回答
举报