I have an array within another array and I would like to condense the subarrays into one object each. Below is the code snippet that generates the initial array:
const final = data.formEntryData.map((item) => {
let key = Object.values(item.data);
let test = data.hmm.map((array) => {
return { [array.name]: key[array.arrayOrder] };
});
return test;
});
Here is the original array structure:
[
// Subarrays here
]
I aim to merge the subarrays so they resemble this format:
[
// Merged objects here
]
I have tried using the reduce method as shown below:
var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = final.reduce(
(obj, item,) => Object.assign(obj, { [item.key]: item.value }), {});
console.log(object)
However, I am struggling because the names for item.key
and item.value
are dynamically derived in the map function. Any assistance on this issue would be highly appreciated. Thank you!