如何在兄弟属性上组合对象属性

How to combine object properties on the sibling properties

我有一个像这样的对象数组,我从一个 csv 文件中读取并解析了这些对象。

const csvDataObjects = [
  {
    Battery: 'Battery',
    batteryDetailsKey: ['Serial Number', 'Type', 'Part Number'],
    batteryDetailsVal: [
      'HJ3CA19347410218LJ98 151 QC',
      'Extended Range',
      '4P94-Q051',
    ],
    Modules: 'Modules',
    moduleDetailsKey: ['Serial Number', 'Part Number', 'Cell Count'],
    moduleDetailsVal: ['8367532735006109322258160 50', 'LJ98-10C779-A51', '32'],
    assetSeparator: 'assetSeparator',
  },
  {
    Battery: 'Battery',
    batteryDetailsKey: ['Serial Number', 'Type', 'Part Number'],
    batteryDetailsVal: [
      'HJ3CA19347410218LJ98 152 QC',
      'Extended Range',
      '4P94-Q052',
    ],
    Modules: 'Modules',
    moduleDetailsKey: ['Serial Number', 'Part Number', 'Cell Count'],
    moduleDetailsVal: [
      ['8367532735006109322258160 51', 'LJ98-10C779-A52', '28'],
      ['8367532735006109322258161 52', 'LJ98-10C779-A53', '27'],
    ],
    assetSeparator: 'assetSeparator',
  },
  {
    Battery: 'Battery',
    batteryDetailsKey: ['Serial Number', 'Type', 'Part Number'],
    batteryDetailsVal: [
      'HJ3CA19347410218LJ98 153 QC',
      'Extended Range',
      '4P94-Q053',
    ],
    Modules: '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'],
    ],
  },
];

我想做的是将所有相关的兄弟对象属性组合在一起。所以我想要电池对象中的电池详细信息,以及模块对象中的模块详细信息。所以最终的输出应该是这样的:

const desiredCsvDataObjects = [
  {
    Battery: {
      batteryDetailsKey: ['Serial Number', 'Type', 'Part Number'],
      batteryDetailsVal: [
        'HJ3CA19347410218LJ98 151 QC',
        'Extended Range',
        '4P94-Q051',
      ],
    },
    Modules: {
      moduleDetailsKey: ['Serial Number', 'Part Number', 'Cell Count'],
      moduleDetailsVal: [
        '8367532735006109322258160 50',
        'LJ98-10C779-A51',
        '32',
      ],
    },
  },
  {
    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'],
      ],
    },
  },
];

这是我试图做我想做的丑陋代码

let newEmptyArr = [];
let emptyObj = {};

csvDataObjects.forEach(item => {
  Object.keys(item).forEach(x => {
    if (x === 'Battery') {
      emptyObj[x] = {};
    }
    if (x === 'batteryDetailsKey') {
      emptyObj.Battery = {
        batteryDetailsKey: item[x],
      };
    }
    if (x === 'batteryDetailsVal') {
      emptyObj.Battery = {
        batteryDetailsVal: item[x],
      };
    }
    if (x === 'Modules') {
      emptyObj[x] = {};
    }
    if (x === 'moduleDetailsKey') {
      emptyObj.Modules = {
        moduleDetailsKey: item[x],
      };
    }
    if (x === 'moduleDetailsVal') {
      emptyObj.Modules = {
        moduleDetailsVal: item[x],
      };
    }
  });
  newEmptyArr.push(emptyObj);
});

请帮我修正我丑陋的代码,或者帮我写一个漂亮漂亮的代码:)

您可以使用 map 从旧数组中获取新数组,如下所示:

const desiredCsvDataObjects  = csvDataObjects.map((item) => {
  const { batteryDetailsKey, batteryDetailsVal, moduleDetailsKey, moduleDetailsVal } = item;
  return {
    Battery: { batteryDetailsKey, batteryDetailsVal },
    Modules: { moduleDetailsKey, moduleDetailsVal },
  };
});

希望对您有所帮助!

使用Array#map()方法:

const result = csvDataObjects.map(
    ({ 
        batteryDetailsKey, 
        batteryDetailsVal, 
        moduleDetailsKey, 
        moduleDetailsVal
    }) => 
    ({
        Battery: { batteryDetailsKey, batteryDetailsVal },
        Modules: { moduleDetailsKey, moduleDetailsVal }
    })
);

演示

const data = [
  {
    Battery: 'Battery',
    batteryDetailsKey: ['Serial Number', 'Type', 'Part Number'],
    batteryDetailsVal: [
      'HJ3CA19347410218LJ98 151 QC',
      'Extended Range',
      '4P94-Q051',
    ],
    Modules: 'Modules',
    moduleDetailsKey: ['Serial Number', 'Part Number', 'Cell Count'],
    moduleDetailsVal: ['8367532735006109322258160 50', 'LJ98-10C779-A51', '32'],
    assetSeparator: 'assetSeparator',
  },
  {
    Battery: 'Battery',
    batteryDetailsKey: ['Serial Number', 'Type', 'Part Number'],
    batteryDetailsVal: [
      'HJ3CA19347410218LJ98 152 QC',
      'Extended Range',
      '4P94-Q052',
    ],
    Modules: 'Modules',
    moduleDetailsKey: ['Serial Number', 'Part Number', 'Cell Count'],
    moduleDetailsVal: [
      ['8367532735006109322258160 51', 'LJ98-10C779-A52', '28'],
      ['8367532735006109322258161 52', 'LJ98-10C779-A53', '27'],
    ],
    assetSeparator: 'assetSeparator',
  },
  {
    Battery: 'Battery',
    batteryDetailsKey: ['Serial Number', 'Type', 'Part Number'],
    batteryDetailsVal: [
      'HJ3CA19347410218LJ98 153 QC',
      'Extended Range',
      '4P94-Q053',
    ],
    Modules: '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'],
    ],
  },
];

const result = data.map(({batteryDetailsKey,batteryDetailsVal,moduleDetailsKey,moduleDetailsVal}) => 
    ({Battery:{batteryDetailsKey,batteryDetailsVal},Modules:{moduleDetailsKey,moduleDetailsVal}})
);

console.log( result );