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 turn 2 arrays into an object with key value pairs

I have an object which contains 2 objects with properties:

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

I need to change the structure of the data to this:

const desiredObject = {
  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',
  },
};

I have tried

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 emptyObject = {}

Object.keys(objects).forEach((q, i) => {
    emptyObject = {
        objects[q] 
    }
})

but isn’t quite achieving what i want, any help is welcome. Thanks

>Solution :

I am assuming that:

  • Both arrays will always have equal lengths. OR
  • Keys’ array (Array 1) is bigger and you want all the keys from Array 1 and all excess keys will have value undefined. OR
  • Keys’ array (Array 1) is smaller and you want to skip all the extra values present in the values’ array (Array 2).

You can try the below code, I have used Array reduce() on array 1 and used the index parameter to access the corresponding value in array 2. For looping the objects object I have used Object.keys() and created a new desired object.

So the complete answer for your problem will be something like below:

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

console.log(desiredObject);
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