我有以下输入数组(这是一个Javascript Object []数组响应):
[
{index:1, headerCSV:"Name", dataFields: ["Name", "Id"]},
{index:2, headerCSV:"Id", dataFields: ["Test", "Test 1"]},
{index:3, headerCSV:"fname", dataFields: ["Test", "Test 1"]},
{index:4, headerCSV:"lname", dataFields: []},
]
我正在尝试将其转换为以下数组:
[
{"header" : 1, "field" :"Name"},
{"header" : 1, "field" :"Id"},
{"header" : 2, "field" :"Test"},
{"header" : 2, "field" :"Test 1"},
{"header" : 3, "field" :"Test"},
{"header" : 3, "field" :"Test 1"}
]
在结果数组中,我需要将header
与输入数组index
放在一起,在field
中,我需要使用{{ 1}}输入数组的数组。我尝试使用以下代码:
dataFields
但是我得到了以下结果:
var CSVHeadersAndFields = /* The input array */;
var headerFieldMappingJSON = [];
for(var i=0; i<CSVHeadersAndFields.length;i++) {
headerFieldMappingJSON[i] = {};
var selectedFields = CSVHeadersAndFields[i].dataFields;
for(var j=0; j<selectedFields.length;j++) {
headerFieldMappingJSON[i].header = CSVHeadersAndFields[i].index;
headerFieldMappingJSON[i].field = selectedFields[j];
}
}
我怀疑将for循环的第一个迭代值替换为第二个迭代,并且我还需要避免从输入数组的[
{"header":1,"field":"Name"},
{"header":2,"field":"Test"},
{"header":3,"field":"Test"},
{}
]
的空数组构造输出数组映射。
如何制定正确的算法来转换数组?
答案 0 :(得分:1)
为结果数组创建一个单独的迭代器索引。或者只是推送到结果数组,而不是按索引添加值:
var CSVHeadersAndFields = /* The array from question */;
var headerFieldMappingJSON = [];
for(var i = 0; i < CSVHeadersAndFields.length; i++) {
var selectedFields = CSVHeadersAndFields[i].dataFields;
for(var j = 0; j < selectedFields.length; j++) {
headerFieldMappingJSON.push({
header: CSVHeadersAndFields[i].index,
field: selectedFields[j]
});
}
}
同一示例,但更简洁:
var input = /* The array from question */;
var output = [];
input.forEach(function (csvRow) {
csvRow.dataFields.forEach(function (field) {
output.push({
header: csvRow.index,
field: field
});
});
});
答案 1 :(得分:1)
下面的代码将为每个dataField
提供一个结果对象,并将index
中的CSVHeadersAndFields
属性用作每个对象的header
属性。
const result = CSVHeadersAndFields.map(item =>
item.dataFields.map(field => {
return {
header: item.index,
field
}
})
);
答案 2 :(得分:1)
您可以尝试以下方法:
obj.dataFields
并为每个字段创建对象。
var data = [
{index:1, headerCSV:"Name", dataFields: ["Name", "Id"] },
{index:2, headerCSV:"Id", dataFields: ["Test", "Test 1"] },
{index:3, headerCSV:"fname", dataFields: ["Test", "Test 1"] },
{index:4, headerCSV:"lname", dataFields: []}
];
var output = data.reduce((acc, obj) => {
const header = obj.index;
return acc.concat( obj.dataFields.map((field) => ({ header, field}) ))
}, []);
console.log(output)
答案 3 :(得分:1)
这是更简单的代码:
var CSVHeadersAndFields = [ {index:1, headerCSV:"Name", dataFields: ["Name","Id"]},
{index:2, headerCSV:"Id", dataFields:["Test","Test 1"]},{index:3, headerCSV:"fname", dataFields:["Test","Test 1"]}, {index:4, headerCSV:"lname", dataFields:[]};
var headerFieldMappingJSON = [];
for(var CSVHeadersAndField of CSVHeadersAndFields ) {
for(var dataFieldVal of CSVHeadersAndField['dataFields']){
headerFieldMappingJSON.push({'header': CSVHeadersAndField['index'], 'field': dataFieldVal })
}
}
输出:
[
{
"header": 1,
"field": "Name"
},
{
"header": 1,
"field": "Id"
},
{
"header": 2,
"field": "Test"
},
{
"header": 2,
"field": "Test 1"
},
{
"header": 3,
"field": "Test"
},
{
"header": 3,
"field": "Test 1"
}
]