如果 item 是数组 [[arr1],[arr2],[arr3,] 的数组,如何创建多个对象值

how to create a multiple object value, if item is an arrary of arrays [[arr1],[arr2],[arr3,]

我有一个这样的对象:

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"],
  },
};

我写了一个函数,让它成为一个key/val对对象

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)

在减少,改变

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)

它适用于两种情况:数组和数组的数组。

您可以使用Array.map().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 数组上工作,这样它会告诉你如何实现它。