Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine object properties on the sibling properties

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 🙂

>Solution :

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading