0

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.?

5 Answers 5

3

You can use the % to always have a correct indice and reuse users.

const users = [
  {
    name: 'John Roberts',
    uid: '49ikds_dm3idmssmmi9sz',
  },
  {
    name: 'Peter Jones',
    uid: 'fmi33_sm39imsz9z9nb',
  }
];

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 += 1){
  cars[i].user = users[i % users.length].uid;
}

console.log(cars);


An ES6 soluce you won't believe! (click bait)

const users = [
  {
    name: 'John Roberts',
    uid: '49ikds_dm3idmssmmi9sz',
  },
  {
    name: 'Peter Jones',
    uid: 'fmi33_sm39imsz9z9nb',
  }
];

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,
  },
];

const ret = cars.map((x, xi) => ({
  ...x,
  
  user: users[xi % users.length].uid,
}));

console.log(ret);

Sign up to request clarification or add additional context in comments.

Comments

1

You just have to test you user[i] var and put a default value:

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] && users[i].uid || /*Default value:*/ null;
}

You can also do it with simple if:

for(let i = 0; i < cars.length; i++){
  //if index is 2 or greater in this example. users[2] will be undefined 
  if (users[i]) {
    cars[i].user = users[i].uid;
  } else {
    cars[i].user = null; // Or another default
  }
}

------------------ Update to take account of comment -----------------

If you want to loop on user and start again the array when you finish you can use modulo %

let user_idx = null
for(let i = 0; i < cars.length; i++){

  user_idx = i % users.length

  //if index is 2 or greater in this example. users[2] will be undefined
  cars[i].user = users[user_idx].uid || null;
}

3 Comments

Thanks for replying Arthur, My problem lies in that I need a user to be assigned to the cars array, it can't be a null value. I need someone way of cycling over my users array once again whilst continuing through my cars loop as normal.
@juicy89 I did an update an use module (rest of divide). In you case we have 3 cars and 2users: 0 with 0, 1 with 1, and 2 with 2.. But user 2 didn't exist, and 2 % 2 (index % user length) = 0.. so car 2 is link to user 0
That's excellent. Thanks Arthur! Never thought to use modulus
0

It's petty simple. Just write following logic.

$.grep(users, function(user, i ) {  cars[i].user = user.uid;});

Comments

0

var users = [
	{
		name: "John Roberts",
		uid: "49ikds_dm3idmssmmi9sz"
	},
	{
		name: "Peter Jones",
		uid: "fmi33_sm39imsz9z9nb"
	}
]

//Actual array lenth is 424
var 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
	}
]

cars.map(function(car){
	var randomIndex = Math.floor(Math.random() * Math.floor(users.length))
	car.user = users[randomIndex].uid;
})
console.log(cars)

Comments

0

option 1:

for(let i = 0; i < cars.length; i++){
   cars[i].user = users[i % users.length].uid;
}

option 2:

for(let i = 0; i < cars.length; i++){
   cars[i].user = users[Math.round(Math.random() * (users.length - 1))].uid;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.