[英]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);
使用map
和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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.