[英]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.
但是,每个过滤器值和以下三个, values , valueLowerBound和valueUpperBound属于同一个模板对象作为过滤器键下的子数组。
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.