如何在对象数组中添加随机值Javascript?

问题描述 投票:0回答:1
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,
  }
];

你好,我想在0-100之间随机添加一个人气分值,随机为产品添加,而不使用函数。

我试图从

https:/levelup.gitconnected.comset-data-structure-in-javascript-62e65908a0e6。

https:/medium.comfront-end-weeklygetting-a-random-item-from-an-array-43e7e18e8796。

但仍然不知道如何在没有 "人气 "元素的情况下,将元素添加到特定的指数中。谢谢

javascript arrays object random javascript-objects
1个回答
2
投票

先将数组过滤到你想要的元素,然后应用随机数的方法来过滤。

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

// call it
addRandomPopularityWhereThereIsNone(products)

请注意,这将修改原始数组。

供参考。


1
投票

请尝试以下解决方案

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);

请看一下 阵列图在运营商


1
投票

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

0
投票

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));
© www.soinside.com 2019 - 2024. All rights reserved.