簡體   English   中英

使用嵌套的對象數組javascript從對象數組中創建csv

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

所以我想把這兩個數據

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
  }
]

並生成一個看起來像這樣的 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,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;
};

您可以對對象、它們的過濾器進行單個循環並迭代鍵。

 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