I have two arrays set up, and my goal is to iterate through the larger array and assign a property to it using randomly selected IDs from the smaller array.
//The actual length of the users array is 140
const users = [
{
name: "John Roberts",
uid: "49ikds_dm3idmssmmi9sz"
},
{
name: "Peter Jones",
uid: "fmi33_sm39imsz9z9nb"
}
]
//The actual length of the cars array 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++){
//In this example, if the index is 2 or higher, users[2] will be undefined
cars[i].user = users[i].uid;
}
Essentially, I am looking for a way to efficiently utilize the small users
array. Once the variable i
reaches 2 or more in the example above, then accessing users[2]
will return undefined
.
Does anyone have a clever solution to suggest that could help me overcome this challenge?