I have 2 arrays structured like so and want to loop through the larger array and assign a property to it, using random ids from a smaller array
//Actual array length is 140
const users = [
{
name: "John Roberts",
uid: "49ikds_dm3idmssmmi9sz"
},
{
name: "Peter Jones",
uid: "fmi33_sm39imsz9z9nb"
}
]
//Actual array lenth is 424
const cars = [
{
manufacturer: "BMW",
model: "320d",
year: "2010",
user: null
},
{
manufacturer: "BMW",
model: "530d",
year: "2018",
user: null
},
{
manufacturer: "AUDI",
model: "RS6",
year: "2014",
user: null
}
]
for(let i = 0; i < cars.length; i++){
//if index is 2 or greater in this example. users[2] will be undefined
cars[i].user = users[i].uid;
}
I basically want to re use the small users array. In the example above, once the variable i is 2 or greater, then users[2] will be undefined.
Can anyone recommend an elegant solution that will help me solve this problem.?