简体   繁体   English

根据索引从两个单独的数组中创建一个对象

[英]Create one object out of two separate arrays based on index

I'm trying to take this array of arrays,我正在尝试使用这个数组数组,

const parsedCsvData = [
  [
    "Passage",
    "Percentage of",
    "constraint2",
    "1",
    "2",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
  ],
  [
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "Avg A Param",
    "",
    "0.3",
    "0.9"
  ],
  [
    "Item",
    "Include",
    "constraint1",
    "1",
    "4",
    "",
    "",
    "",
    "",
    "",
    "",
    ""
  ],
  [
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "Item Identifier",
    "I105_15201",
    "",
    ""
  ]
]

and populate this object with their values并用它们的值填充这个对象

const template = {
  "attributeName": "",
  "constraintType": "",
  "objectType": "",
  "filters": [
    {
      "objectType": "",
      "attributeName": "",
      "values": "",
      "valueLowerBound": "",
      "valueUpperBound": ""
    }
  ],
  "upperBound": "",
  "description": "",
  "lowerBound": ""
}

each array from parsedCsvData was a row in a CSV file I parsed. parsedCsvData 中的每个数组都是我解析的 CSV 文件中的一行。 Each value in the array of arrays is indexed based off another array数组数组中的每个值都基于另一个数组进行索引

const csvHeaders = [
  "objectType",
  "constraintType",
  "description",
  "lowerBound",
  "upperBound",
  "attributeName",
  "referenceValue",
  "scope",
  "filters",
  "values",
  "valueLowerBound",
  "valueUpperBound"
]

For example, in the first sub array of parsedCsvData Passage is at index 0 and the value for objectType in csvHeaders.例如,在 parsedCsvData Passage的第一个子数组中,索引为 0 并且 csvHeaders 中的objectType值。 Similarly, description is at index 2 and is constraint1 and constraint2 respectively.类似地,描述位于索引 2 处,分别是约束 1约束2。

Additionally, if there exists a filters value at index 8 in the csv a new row was created.此外,如果 csv 中的索引 8 处存在过滤器值,则会创建一个新行。 After parsing, this created a new sub array for each filter value.解析后,这为每个过滤器值创建了一个新的子数组。 However, each filters value and the following three, values , valueLowerBound and valueUpperBound belong in the same template object as a subarray under the filters key.但是,每个过滤器值和以下三个, valuesvalueLowerBoundvalueUpperBound属于同一个模板对象作为过滤器键下的子数组。

Here is an example of the output I am trying to accomplish given what I posted above.鉴于我上面发布的内容,这是我试图完成的输出示例。 Never mind the empty key/value pairs.别介意空的键/值对。

[{
  "constraintType": "Percentage of",
  "objectType": "Passage",
  "filters": [
    {
      "attributeName": "Avg A Param",
      "values": "",
      "valueLowerBound": "0.3",
      "valueUpperBound": "0.9"
    } // there may be multiple filter objects as part of this array
  ],
  "upperBound": "2",
  "description": "constraint2",
  "lowerBound": "1"
},
{
  "constraintType": "Include",
  "objectType": "Item",
  "filters": [
    {
      "attributeName": "Item Identifier",
      "values": "I105_15201",
      "valueLowerBound": "",
      "valueUpperBound": ""
    }
  ],
  "upperBound": "4",
  "description": "constraint1",
  "lowerBound": "1"
}
]

I've tried mapping the csvHeaders array, attempting to use their indices and then do a map of the the parsedCsvData values and assign them to the template object, but can't seem to get it to work.我尝试映射 csvHeaders 数组,尝试使用它们的索引,然后对 parsedCsvData 值进行映射并将它们分配给模板对象,但似乎无法让它工作。 Any help would be much appreciated.任何帮助将非常感激。

An approach by checking the first element of the nested arra for either assign a new object or assign a new nested object.通过检查嵌套 arra 的第一个元素来分配新对象或分配新嵌套对象的方法。

 const parsedCsvData = [["Passage", "Percentage of", "constraint2", "1", "2", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", "", "Avg A Param", "", "0.3", "0.9"], ["Item", "Include", "constraint1", "1", "4", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", "", "Item Identifier", "I105_15201", "", ""]], template = ["objectType", "constraintType", "description", "lowerBound", "upperBound", "xxxx", "referenceValue", "scope", "attributeName", "values", "valueLowerBound", "valueUpperBound"], result = parsedCsvData.reduce((r, a) => { const assign = (object, target, value) => { const keys = target.split('.'), last = keys.pop(); keys.reduce((o, k) => o[k] = o[k] || {}, object)[last] = value; }; if (a[0]) { r.push(a.reduce((t, v, i) => { if (v) assign(t, template[i], v); return t; }, {})); } else { r[r.length - 1].filters = r[r.length - 1].filters || []; r[r.length - 1].filters.push(a.reduce((t, v, i) => { if (v) assign(t, template[i], v); return t; }, {})); } return r; }, []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

暂无
暂无

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

相关问题 在JavaScript中使用两个数组的字符串索引创建对象 - Create object with String index of two arrays in javascript 坚持基于来自两个单独数组的日期构建对象 - Stuck with building an object based on date from two separate arrays 如何基于从 Javascript/ES6 中的一个数组中使用过滤条件过滤两个数组来创建对象? - How to create an object based on filtering two arrays with a filter criteria from one array in Javascript/ES6? 合并来自基于索引的两个对象数组的对象 - Merge objects from two arrays of object based in the index 基于两个单独的数组构造一个数组 - Construct an array based on two separate arrays 基于两个object arrays创建html表:一个包含数据,另一个包含列(包括数据数组属性名称) - Create html table based on two object arrays: one containing the data, and the other including the columns (including the data array attribute names) 如何从两个单独的数组创建具有两个键/值对的对象 - How to create an object with two key/value pairs from two separate arrays 从以下 JSON object 作为两个单独的 arrays 获取数据的最有效方法是什么? - What is the most efficient way to get data out of the following JSON object as two separate arrays? 基于两个数组创建一个数组 - Create an array based on two arrays 将两个单独的数组与对象组合成一个数组 - combining two separate arrays into one array with objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM