jeudi 10 décembre 2020

Convert a enum JavaScript to array, and select a random element in array

I have the following problem, I need to transform an enum (object) into an array and thus search for a random element within the array whenever I create a new record. The problem is that it is always creating the same category during all executions of the loop.

categoryCars = {
    TOPCAR: 'topCar',
    MAXCAR: 'maxCar',
    NEWCAR: 'newCar'
}

const cars = []
  var categories = categoryCars

  var categoryArray = Object.values(categories)

  var categoryRandom = categoryArray[Math.floor(Math.random() * categoryArray.length)]

  for (let i = 0; i <= 100; i++) {
    cars.push({
      buyer: buyerId,
      category: categoryRandom,
      position: Math.floor(Math.random() * 3) + 1,
      date_created: 20201210
    })
  }

  console.log(cars)

What am I doing wrong? Can you help me?




Aucun commentaire:

Enregistrer un commentaire