How to turn 2 arrays into an object with key value pairs

Jim41Mavs

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

let emptyObject = {}

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

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

EDIT:

I just realised that for moduleDetailsVal sometimes the value is an array of arrays like this:

const objectsWithMultipleArrays = {
  Battery: {
    batteryDetailsKey: ['serial_no', 'type', 'part_no'],
    batteryDetailsVal: [
      'HJ3CA19347410218LJ98 151 QC',
      'Extended Range',
      '4P94-Q051',
    ],
  },
  Modules: {
    moduleDetailsKey: ['serial_no', 'part_no', 'Cell Count'],
    moduleDetailsVal: [
    [
        "8367532735006109322258162 53",
        "LJ98-10C779-A54",
        "28",
    ],
    [
        "8367532735006109322258163 54",
        "LJ98-10C779-A55",
        "27",
    ],
    [
        "8367532735006109322258163 56",
        "LJ98-10C779-A56",
        "27"
    ]
],
  },
};

The desire is now for that object to have an array of objects like this

const newDesiredObject = {
  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"
    },

  ]
}

Please advise how i can handle this case

Shivam Sharma

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324299801&siteId=291194637