I have an object like this:
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 have written a function so that it becomes a key/val pair object
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]]);
});
it does give me what i want with this data structure:
{
"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"
}
}
However there's an issue in the code when moduleDetailsVal has multiple arrays rather than just 1 array with values:
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"],
["8367532735006109322258160 51", "LJ98-10C779-A52", "33", "6"],
["8367532735006109322258160 52", "LJ98-10C779-A52", "34", "6"],
]
},
};
My code is only reading one moduleDetailsVal, but in reality it should read all of them, and there could be numerous amounts of Arrays inside moduleDetailsVal, not just 2 or 3
I ideally want it too look like this:
{
"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"
},
]
}
CodePudding user response:
In the reduce, change
obj[item] = arr2[index];
to:
obj[item] = Array.isArray(arr2[index])
? arr2.map(a => a[index])
: arr2[index]
So you'll have:
function twoArraysToObject(arr1, arr2) {
return arr1.reduce((obj, item, index) => {
obj[item] = Array.isArray(arr2[index])
? arr2.map(a => a[index])
: 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]]);
});
CodePudding user response:
You can achieve it in a very simple way by using Array.map()
and .forEach()
method :
const modules = {
moduleDetailsKey: ["serial_no", "part_no", "cell_count"],
moduleDetailsVal: [
["8367532735006109322258160 50", "LJ98-10C779-A51", "32", "6"],
["8367532735006109322258160 51", "LJ98-10C779-A52", "33", "6"],
["8367532735006109322258160 52", "LJ98-10C779-A52", "34", "6"],
]
};
const res = modules.moduleDetailsVal.map((arr) => {
const obj = {};
modules.moduleDetailsKey.forEach((key, index) => {
obj[key] = arr[index]
});
return obj;
});
console.log(res);
Just for Demo purpose, I worked on modules
array so that it will give you the way how to achieve it.