如何解决使用对象嵌套Javascript从对象数组创建csv
所以我试图获取这两段数据
const headers = [
{ label: 'Item/Passage',key: 'objectType' },{ label: 'Constraint Type',key: 'constraintType' },{ label: 'Constraint Name',key: 'description' },{ label: 'Lower',key: 'lowerBound' },{ label: 'Upper',key: 'upperBound' },{ label: 'Target Attribute',key: 'attributeName' },{ label: 'Theta',key: 'referenceValue' },{ label: 'Form/Passage',key: 'scope' },{ label: 'Filter',key: 'filters' },{ label: 'Filter values',key: 'filterValues' },{ label: 'Min',key: 'min' },{ label: 'Max',key: 'max' },];
const array = [
{
"objectType": "Item","constraintType": "Include","description": "constraint1","lowerBound": "1","filters": [
{
"attributeName": "Item Identifier","values": "I105_15201|I105_15202","valueLowerBound": null,"valueUpperBound": null
},{
"attributeName": "Passage Item Order","values": "5|1|3|4|6|7|8|9|10|11|12|13|14|15|16|None","valueUpperBound": null
}
],"upperBound": "4","attributeName": null,"referenceValue": "","scope": null
},{
"objectType": "Passage","description": "constraint2","filters": [
{
"attributeName": "Passage Identifier","values": "pid_1-1|pid_10-1|pid_2-1|pid_4-1|pid_5-1|pid_7-1|pid_8-1|pid_9-1",{
"attributeName": "Word Count","values": "","valueLowerBound": 3,"valueUpperBound": 234
}
],{
"objectType": "Item","description": "constraint3","filters": [],"scope": null
}
]
并生成一个如下所示的csv文件
基本上,我从标头数组中获取标签值,创建标头,然后将每个约束放在自己的行上。如果约束条件具有过滤器,则会创建另一行,并将值放在最后四列内。例如,约束1和2有两个过滤器,约束3没有任何过滤器。
我已经可以使用以下代码完成操作,但是觉得它不是一个非常稳定的实现。寻找有关如何实施此建议。谢谢!
这是我已经能够创建的字符串的副本
toCsv -> csv Item/Passage,Constraint Type,Constraint Name,Lower,Upper,Target Attribute,Theta,Form/Passage,Filter,Filter values,Min,Max
Item,Include,constraint1,1,4,Item Identifier,I105_15201|I105_15202,Passage Item Order,5|1|3|4|6|7|8|9|10|11|12|13|14|15|16|None,Passage,constraint2,Passage Identifier,pid_1-1|pid_10-1|pid_2-1|pid_4-1|pid_5-1|pid_7-1|pid_8-1|pid_9-1,Word Count,3,234
Item,constraint3,
export const toCsv = (array,headers) => {
const getValuesFromObject = (obj) => {
if (typeof obj !== 'object' || obj === null) {
return [];
}
const headerKeys = Object.keys(headers);
const keys = Object.keys(obj);
const values = [];
const filterValues = [];
for (var i = 0; i < keys.length; ++i) {
if (Array.isArray(obj[keys[i]])) {
obj[keys[i]].map((filterObj) => {
filterValues.push(',' + Object.values(filterObj)); // this part bothers me the most. I use it to create empty cells for the filter rows.
});
} else {
values.push(obj[keys[i]]);
}
}
return [].concat([values]).concat(filterValues).join('\n');
};
const csvHeaders = headers.map((obj) => obj.label).join(',');
const body = array.map(getValuesFromObject);
let csv = [].concat([csvHeaders]).concat(body).join('\n');
return csv;
};
解决方法
您可以对对象,它们的过滤器进行一次循环,并迭代键。
const
toCsv = (array,headers) => {
const getValuesFromObject = o => !o || typeof o !== 'object'
? []
: [
headers.map(({ key }) => key !== 'filters' && o[key] || '').join(),...o.filters.map(q => headers.map(({ key }) => q[key] || '').join())
];
return [
headers.map((obj) => obj.label).join(','),...array.flatMap(getValuesFromObject)
].join('\n');
},headers = [{ label: 'Item/Passage',key: 'objectType' },{ label: 'Constraint Type',key: 'constraintType' },{ label: 'Constraint Name',key: 'description' },{ label: 'Lower',key: 'lowerBound' },{ label: 'Upper',key: 'upperBound' },{ label: 'Target Attribute',key: 'attributeName' },{ label: 'Theta',key: 'referenceValue' },{ label: 'Form/Passage',key: 'scope' },{ label: 'Filter',key: 'filters' },{ label: 'Filter values',key: 'values' },{ label: 'Min',key: 'valueLowerBound' },{ label: 'Max',key: 'valueUpperBound' }],array = [{ objectType: "Item",constraintType: "Include",description: "constraint1",lowerBound: "1",filters: [{ attributeName: "Item Identifier",values: "I105_15201|I105_15202",valueLowerBound: null,valueUpperBound: null },{ attributeName: "Passage Item Order",values: "5|1|3|4|6|7|8|9|10|11|12|13|14|15|16|None",valueUpperBound: null }],upperBound: "4",attributeName: null,referenceValue: "",scope: null },{ objectType: "Passage",description: "constraint2",filters: [{ attributeName: "Passage Identifier",values: "pid_1-1|pid_10-1|pid_2-1|pid_4-1|pid_5-1|pid_7-1|pid_8-1|pid_9-1",{ attributeName: "Word Count",values: "",valueLowerBound: 3,valueUpperBound: 234 }],{ objectType: "Item",description: "constraint3",filters: [],scope: null }],result = toCsv(array,headers);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。