简体   繁体   English

使用嵌套的对象数组javascript从对象数组中创建csv

[英]Create csv out of array of objects with nested array of objects javascript

So I'm trying to take these two pieces of data所以我想把这两个数据

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",
        "valueLowerBound": null,
        "valueUpperBound": null
      }
    ],
    "upperBound": "4",
    "attributeName": null,
    "referenceValue": "",
    "scope": null
  },
  {
    "objectType": "Passage",
    "constraintType": "Include",
    "description": "constraint2",
    "lowerBound": "1",
    "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",
        "valueLowerBound": null,
        "valueUpperBound": null
      },
      {
        "attributeName": "Word Count",
        "values": "",
        "valueLowerBound": 3,
        "valueUpperBound": 234
      }
    ],
    "upperBound": "4",
    "attributeName": null,
    "referenceValue": "",
    "scope": null
  },
  {
    "objectType": "Item",
    "constraintType": "Include",
    "description": "constraint3",
    "filters": [],
    "lowerBound": "1",
    "upperBound": "4",
    "attributeName": null,
    "referenceValue": "",
    "scope": null
  }
]

And produce a csv file that looks like this,并生成一个看起来像这样的 csv 文件,

在此处输入图片说明

Basically, I take the label values from the header array, create the header, then each constraint is put on its own row.基本上,我从标题数组中获取标签值,创建标题,然后将每个约束放在自己的行上。 If a constraint has filters, another row is created and the values are placed within the last four columns.如果约束具有过滤器,则会创建另一行并将值放置在最后四列中。 For example, constraint1 and 2 have two filters, constraint 3 has none.例如,约束 1 和 2 有两个过滤器,约束 3 没有。

I've been able to accomplish already with the following code, but feel it's not a terribly stable implementation.我已经能够使用以下代码完成,但感觉它不是一个非常稳定的实现。 Looking for any suggestions on how to implement this.寻找有关如何实现这一点的任何建议。 Thanks!谢谢!

Here is the copy of the string I've been able to create这是我能够创建的字符串的副本

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,Include,constraint2,1,4,,,
,,,,,,,,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,Include,constraint3,1,4,,,
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;
};

You could take a single loop for the objects, their filters and iterate the keys.您可以对对象、它们的过滤器进行单个循环并迭代键。

 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", valueLowerBound: null, valueUpperBound: null }], upperBound: "4", attributeName: null, referenceValue: "", scope: null }, { objectType: "Passage", constraintType: "Include", description: "constraint2", lowerBound: "1", 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", valueLowerBound: null, valueUpperBound: null }, { attributeName: "Word Count", values: "", valueLowerBound: 3, valueUpperBound: 234 }], upperBound: "4", attributeName: null, referenceValue: "", scope: null }, { objectType: "Item", constraintType: "Include", description: "constraint3", filters: [], lowerBound: "1", upperBound: "4", attributeName: null, referenceValue: "", scope: null }], result = toCsv(array, headers); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM