Home > Enterprise >  How to combine object properties on the sibling properties
How to combine object properties on the sibling properties

Time:03-23

I have an array of objects like this which i've read and parsed from a csv file.

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

What i want to do is combine all the related sibling object properties to be combined together. So i want the Battery details in the Battery object, and the Modules detail in modules object. So the final output should look like this:

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

Here is my ugly code attempting to do what i want

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);
});

Please help me fix my ugly code, or help me write a nice pretty one :)

CodePudding user response:

You can use map to get a new array from the old array like this:

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

I hope it helpful!

CodePudding user response:

Use Array#map() method:

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

DEMO

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 );

  • Related