我有一個這樣的物件:
const objects = {
Battery: {
batteryDetailsKey: ["serial_no", "type", "part_no"],
batteryDetailsVal: ["HJ3CA19347410218LJ98 151 QC", "Extended Range", "4P94-Q051"],
},
Modules: {
moduleDetailsKey: ["serial_no", "part_no", "cell_count"],
moduleDetailsVal: ["8367532735006109322258160 50", "LJ98-10C779-A51", "32", "6"],
},
};
我撰寫了一個函式,使其成為鍵/值對物件
function twoArraysToObject(arr1, arr2) {
return arr1.reduce((obj, item, index) => {
obj[item] = arr2[index];
return obj;
}, {});
}
const desiredObject = {};
Object.keys(objects).forEach((key) => {
const keyNamesArr = Object.keys(objects[key]);
desiredObject[key] = twoArraysToObject(objects[key][keyNamesArr[0]], objects[key][keyNamesArr[1]]);
});
它確實為我提供了我想要的資料結構:
{
"Battery": {
"serial_no": "HJ3CA19347410218LJ98 151 QC",
"type": "Extended Range",
"part_no": "4P94-Q051"
},
"Modules": {
"serial_no": "8367532735006109322258160 50",
"part_no": "LJ98-10C779-A51",
"cell_count": "32"
}
}
但是,當 moduleDetailsVal 具有多個陣列而不是只有 1 個具有值的陣列時,代碼中會出現問題:
const objects = {
Battery: {
batteryDetailsKey: ["serial_no", "type", "part_no"],
batteryDetailsVal: ["HJ3CA19347410218LJ98 151 QC", "Extended Range", "4P94-Q051"],
},
Modules: {
moduleDetailsKey: ["serial_no", "part_no", "cell_count"],
moduleDetailsVal: [
["8367532735006109322258160 50", "LJ98-10C779-A51", "32", "6"],
["8367532735006109322258160 51", "LJ98-10C779-A52", "33", "6"],
["8367532735006109322258160 52", "LJ98-10C779-A52", "34", "6"],
]
},
};
我的代碼只讀取了一個moduleDetailsVal,但實際上它應該讀取所??有這些,并且moduleDetailsVal中可能有大量的陣列,而不僅僅是2個或3個
我理想情況下也希望它看起來像這樣:
{
"Battery": {
"serial_no": "HJ3CA19347410218LJ98 151 QC",
"type": "Extended Range",
"part_no": "4P94-Q051"
},
"Modules": [
{
"serial_no": "8367532735006109322258160 50",
"part_no": "LJ98-10C779-A51",
"cell_count": "32"
},
{
"serial_no": "8367532735006109322258160 51",
"part_no": "LJ98-10C779-A52",
"cell_count": "33"
},
{
"serial_no": "8367532735006109322258160 52",
"part_no": "LJ98-10C779-A53",
"cell_count": "33"
},
]
}
將我的完整代碼添加為@re-za 的答案并不是 100% 按預期作業。
const csvDataObjects = [
{
"Battery": {
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number",
"Length",
"Width",
"Height",
"Weight",
"Date Manufactured",
"Manufacturer",
"Capacity",
"Design Capacity",
"Capacity Percentage",
"Temperature",
"Cycle Count",
"Recharge Level Percentage",
"Wear Level Percentage",
"Date Taken on",
"Bill of Materials",
"Bill of Landing",
"Safety Sheet",
"Asset Status"
],
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 151 QC",
"Extended Range",
"4P94-Q051",
"40",
"68",
"11",
"600",
"15 January 2021",
"Ford Motor Company",
"68",
"88",
"100",
"68",
"0",
"100",
"0",
"3 March 2021",
"bill_of_materials_for_this_battery.pdf",
"bill_of_lading_for_this_battery.pdf",
"safety_sheet_for_this_type.pdf",
"End of life"
]
},
"Modules": {
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count",
"Length",
"Width",
"Height",
"Weight",
"Date Manufactured",
"Manufacturer",
"Short Form Name",
"Chemical Full Name",
"Formula",
"Cobalt Percentage",
"Nickel Content",
"Lithium Content",
"Cobalt Content",
"Attached To Battery",
"Asset Status",
"",
"",
""
],
"moduleDetailsVal": [
"8367532735006109322258160 50",
"LJ98-10C779-A51",
"32",
"6",
"19",
"4",
"35",
"16 December 2020",
"LG Chem",
"NCM-111",
"Lithium Nickel Manganese Cobalt Oxide",
"LiNiMnCoO",
"33",
"56",
"50",
"54",
"true",
"End of life",
"",
"",
""
]
}
},
{
"Battery": {
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number",
"Length",
"Width",
"Height",
"Weight",
"Date Manufactured",
"Manufacturer",
"Capacity",
"Design Capacity",
"Capacity Percentage",
"Temperature",
"Cycle Count",
"Recharge Level Percentage",
"Wear Level Percentage",
"Date Taken on",
"Bill of Materials",
"Bill of Landing",
"Safety Sheet",
"Asset Status"
],
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 152 QC",
"Extended Range",
"4P94-Q052",
"50",
"77",
"12",
"680",
"6 June 2020",
"Ford Motor Company",
"71",
"73",
"96",
"67",
"0",
"98",
"2",
"3 July 2020",
"bill_of_materials_for_this_battery_2.pdf",
"bill_of_lading_for_this_battery_2.pdf",
"safety_sheet_for_this_type_2.pdf",
"End of life"
]
},
"Modules": {
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count",
"Length",
"Width",
"Height",
"Weight",
"Date Manufactured",
"Manufacturer",
"Short Form Name",
"Chemical Full Name",
"Formula",
"Cobalt Percentage",
"Nickel Content",
"Lithium Content",
"Cobalt Content",
"Attached To Battery",
"Asset Status",
"",
"",
""
],
"moduleDetailsVal": [
[
"8367532735006109322258160 51",
"LJ98-10C779-A52",
"28",
"10",
"22",
"5",
"42",
"7 April 2020",
"LG Chem",
"NCM-111",
"Lithium Nickel Manganese Cobalt Oxide",
"LiNiMnCoO",
"31",
"59",
"44",
"60",
"true",
"End of life",
"",
"",
""
],
[
"8367532735006109322258161 52",
"LJ98-10C779-A53",
"27",
"11",
"20",
"6",
"43",
"7 April 2020",
"LG Chem",
"NCM-111",
"Lithium Nickel Manganese Cobalt Oxide",
"LiNiMnCoO",
"31",
"59",
"44",
"60",
"true",
"End of life",
"",
"",
""
]
]
}
},
{
"Battery": {
"batteryDetailsKey": [
"Serial Number",
"Type",
"Part Number",
"Length",
"Width",
"Height",
"Weight",
"Date Manufactured",
"Manufacturer",
"Capacity",
"Design Capacity",
"Capacity Percentage",
"Temperature",
"Cycle Count",
"Recharge Level Percentage",
"Wear Level Percentage",
"Date Taken on",
"Bill of Materials",
"Bill of Landing",
"Safety Sheet",
"Asset Status"
],
"batteryDetailsVal": [
"HJ3CA19347410218LJ98 153 QC",
"Extended Range",
"4P94-Q053",
"50",
"77",
"12",
"680",
"6 June 2020",
"Ford Motor Company",
"71",
"73",
"96",
"67",
"0",
"98",
"2",
"3 July 2020",
"bill_of_materials_for_this_battery_2.pdf",
"bill_of_lading_for_this_battery_2.pdf",
"safety_sheet_for_this_type_2.pdf",
"End of life"
]
},
"Modules": {
"moduleDetailsKey": [
"Serial Number",
"Part Number",
"Cell Count",
"Length",
"Width",
"Height",
"Weight",
"Date Manufactured",
"Manufacturer",
"Short Form Name",
"Chemical Full Name",
"Formula",
"Cobalt Percentage",
"Nickel Content",
"Lithium Content",
"Cobalt Content",
"Attached To Battery",
"Asset Status",
"",
"",
""
],
"moduleDetailsVal": [
[
"8367532735006109322258162 53",
"LJ98-10C779-A54",
"28",
"10",
"22",
"5",
"42",
"7 April 2020",
"LG Chem",
"NCM-111",
"Lithium Nickel Manganese Cobalt Oxide",
"LiNiMnCoO",
"31",
"59",
"44",
"60",
"true",
"End of life",
"",
"",
""
],
[
"8367532735006109322258163 54",
"LJ98-10C779-A55",
"27",
"11",
"20",
"6",
"43",
"7 April 2020",
"LG Chem",
"NCM-111",
"Lithium Nickel Manganese Cobalt Oxide",
"LiNiMnCoO",
"31",
"59",
"44",
"60",
"true",
"End of life",
"",
"",
""
],
[
"8367532735006109322258163 56",
"LJ98-10C779-A56",
"27",
"11",
"20",
"6",
"43",
"7 April 2020",
"LG Chem",
"NCM-111",
"Lithium Nickel Manganese Cobalt Oxide",
"LiNiMnCoO",
"31",
"59",
"44",
"60",
"true",
"End of life",
"",
"",
""
]
]
}
}
]
const mappedObjects = csvDataObjects.map((csvObj) => {
function twoArraysToObject(arr1, arr2) {
return arr1.reduce((obj, item, index) => {
obj[item] = Array.isArray(arr2[index])
? arr2.map((a) => a[index])
: arr2[index]
return obj;
}, {});
}
const desiredObject = {};
Object.keys(csvObj).forEach((key) => {
const keyNamesArr = Object.keys(csvObj[key]);
desiredObject[key] = twoArraysToObject(csvObj[key][keyNamesArr[0]], csvObj[key][keyNamesArr[1]]);
});
Object.keys(desiredObject).forEach(key => {
if (desiredObject[key] === '') {
delete desiredObject[key];
}
});
return desiredObject
});
console.log(mappedObjects)
uj5u.com熱心網友回復:
在減少,改變
obj[item] = arr2[index];
到:
obj[item] = Array.isArray(arr2[0])
? arr2.map(a => a[index])
: arr2[index]
所以你會有:
function twoArraysToObject(arr1, arr2) {
return arr1.reduce((obj, item, index) => {
if (item === "") return obj
obj[item] = Array.isArray(arr2[0])
? arr2.map(a => a[index])
: arr2[index]
return obj;
}, {});
}
作業示例
const csvDataObjects = [
{
"Battery": {
"batteryDetailsKey": ["Serial Number", "Type", "Part Number"],
"batteryDetailsVal": ["HJ3CA19347410218LJ98 152 QC", "Extended Range", "4P94-Q052"]
},
"Modules": {
"moduleDetailsKey": ["Serial Number", "Part Number", "Cell Count", ""],
"moduleDetailsVal": [
["8367532735006109322258160 51", "LJ98-10C779-A52", "28", ""],
["8367532735006109322258161 52", "LJ98-10C779-A53", "27", ""]
]
}
},
{
"Battery": {
"batteryDetailsKey": ["Serial Number", "Type", "Part Number"],
"batteryDetailsVal": ["HJ3CA19347410218LJ98 153 QC", "Extended Range", "4P94-Q053"]
},
"Modules": {
"moduleDetailsKey": ["Serial Number", "Part Number", "Cell Count", ""],
"moduleDetailsVal": [
["8367532735006109322258162 53", "LJ98-10C779-A54", "28", ""],
["8367532735006109322258163 54", "LJ98-10C779-A55", "27", ""],
["8367532735006109322258163 56", "LJ98-10C779-A56", "27", ""]
]
}
}
]
function twoArraysToObject(arr1, arr2) {
return arr1.reduce((obj, item, index) => {
if (item === "") return obj
obj[item] = (Array.isArray(arr2[0]))
? arr2.map((a) => a[index])
: obj[item] = arr2[index]
return obj;
}, {});
}
const mappedObjects = csvDataObjects.map((csvObj) => {
const desiredObject = {};
Object.keys(csvObj).forEach((key) => {
const keyNamesArr = Object.keys(csvObj[key]);
desiredObject[key] = twoArraysToObject(csvObj[key][keyNamesArr[0]], csvObj[key][keyNamesArr[1]]);
});
return desiredObject
});
console.log(mappedObjects)
它適用于兩種情況:陣列和陣列陣列。
uj5u.com熱心網友回復:
您可以使用Array.map()and.forEach()方法以非常簡單的方式實作它:
const modules = {
moduleDetailsKey: ["serial_no", "part_no", "cell_count"],
moduleDetailsVal: [
["8367532735006109322258160 50", "LJ98-10C779-A51", "32", "6"],
["8367532735006109322258160 51", "LJ98-10C779-A52", "33", "6"],
["8367532735006109322258160 52", "LJ98-10C779-A52", "34", "6"],
]
};
const res = modules.moduleDetailsVal.map((arr) => {
const obj = {};
modules.moduleDetailsKey.forEach((key, index) => {
obj[key] = arr[index]
});
return obj;
});
console.log(res);
僅出于演示目的,我研究了modules陣列,以便它為您提供實作它的方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/448358.html
