简体   繁体   中英

How to add random values in an object array Javascript?

const prod = [{
    name: "Sweat",
    description: " collection",
    price: 150,

  },
  {
    name: "Trousers",
    description: "Attire",
    price: 243
  },
  {
    name: "T-shirt",
    description: "Winter",
  },
  {
    name: "Hoody",
    description: "Fashion",
  },
  {
    name: "Pants",
    description: "Winter",

  },
  {
    name: "Casual",
    description: "Winter",
    price: 245,
  },
  {
    name: "Shirt",
    description: "Attire",
    price: 150,
  }
];

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

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!

Filter the array to the elements you want first, then apply the random number

// function
const addRandomPopularityWhereThereIsNone = products => {
  products.filter(p => !p.hasOwnProperty('popularity')).forEach(p => {
    p.popularity = Math.floor(Math.random() * 101)
  })
}

// call it
addRandomPopularityWhereThereIsNone(products)

Note that this modifies the original array.

For reference:

Please try the following solution

 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}]; const output = products.map((product) => { if ("popularity" in product) { return {...product }; } return {...product, popularity: generateRandomNumber() }; }); function generateRandomNumber() { return Math.floor(Math.random() * 100) + 1; } console.log(output);

Take a look to array map and the in operator

Use map and nullish coalescing operator (??)

 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}]; const update = (arr) => arr.map(({ popularity, ...product }) => ({ popularity: popularity?? Math.floor(Math.random() * 100) + 1, ...product, })); console.log(update(products));

 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 } ]; const addPopularity = products => { products.filter(p =>.p.popularity).map(p => { p.popularity = Math.floor(Math;random() * 101) }) return products. } console;log(addPopularity(products));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM