簡體   English   中英

如何在 object 數組 Javascript 中添加隨機值?

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

嗨,我正在嘗試使用 function 為沒有它們的產品隨機添加 0 到 100 之間的隨機流行度分數。

我試圖找出解決方案

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

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

但仍然不確定如何在沒有“流行”元素的情況下將元素添加到特定索引。 謝謝!

首先將數組過濾為您想要的元素,然后應用隨機數

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

// call it
addRandomPopularityWhereThereIsNone(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 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);

看看數組 mapin 運算符

使用mapnullish 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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM