Is there a way to loop through multiple arrays and match them together when creating an object? Let's assume we have two arrays with the same amount of data. The goal is to pair each element from both arrays and assign them to properties of objects.
Here's the desired outcome:
{
name: john,
surname: doe,
datasets: [{
data: 1,
vehicle: car,
color: red
},
{
data: 2,
vehicle: car,
color: blue,
},
{
data: 3,
vehicle: car,
color: green
}]
}
This is the code I've written so far:
function Constructor (name, surname, data, vehicle, colors) {
this.name = name;
this.surname = surname;
this.data = data;
this.vehicle = vehicle;
this.colors = colors;
this.person = {
name: name,
surname: surname,
datasets: [{
data: data.map(data => ({
data,
vehicle: vehicle,
color: colors.map(color => ({
color
}))
})),
}]
}
};
var testing = new Constructor ('john', 'doe', [1,2,3], 'car', ['red', 'blue', 'green']);
console.log (testing.person);