I am trying to resolve two arrays of length n into n objects with the same number of key value pairs as there are arrays. Each object should have a dynamic name key that correlates to the object's position.
For example, if there are 10 items in each array, there should be 10 objects and each objects should have a name key the is sequential --> Day[n]
The input will look like this:
// input
const temperatures: [65, 33]
const rain: [3, 8]
The output shape should look something like this:
// output
[
{
name: 'Day1',
temperature: 65,
rain: 3
},
{
name: 'Day2',
temperature: 33,
rain: 8
},
]
I've tried mapping through each array and creating an object for each array item and then combining them into one array of objects, but for some reasson, the results array only includes one set of the array names.
This is my code so far:
const temperature = [65, 33];
const rain = [3, 8];
const days = { name: 'Day1' };
const data1 = temperature.map((val) => {
return { temperature: val };
});
const data2 = rain.map((val) => {
return { rain: val };
});
const total = { ...days, ...data1, ...data2 };
console.log(total);
// output: { '0': { temperature: 65 }, '1': { temperature: 33 }, name: 'Day1' }
NOTE: I am creating the input arrays, so if there is any data I need to add or make changes to, that is a possibility.