mercredi 10 juin 2020

How to add random values in an object array Javascript?

const products = [{
    name: "Pullover Sweat",
    description: "Winter collection",
    price: 150,
    popularity: 99
  },
  {
    name: "Formal Trousers",
    description: "Attire for men",
    price: 500
  },
  {
    name: "Winter T-shirt",
    description: "Winter collection",
    price: 50,
    popularity: 50
  },
  {
    name: "New Fashion Hoody",
    description: "Fashion line",
    price: 200
  },
  {
    name: "Winter Pants",
    description: "Winter collection",
    price: 150
  },
  {
    name: "Casual Coat",
    description: "Winter collection",
    price: 245,
    popularity: 78
  },
  {
    name: "Fine Long Sleeve Shirt",
    description: "Attire for men",
    price: 150,
    popularity: 10
  }
];

Hi, I'm trying to add a random popularity score between 0 - 100, randomly for the products without them.

I've tried to figure out solutions from

https://levelup.gitconnected.com/set-data-structure-in-javascript-62e65908a0e6

and

https://medium.com/front-end-weekly/getting-a-random-item-from-an-array-43e7e18e8796

but still unsure how to add elements to specific indices without the 'popularity' element. Thanks!




Aucun commentaire:

Enregistrer un commentaire