0
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!

4 Answers 4

2

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:

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the fast response! I'll give it a try, how would that look as a function as well?
@PabloJr.Villasenor I don't quite understand what you're asking
My bad, the problem I'm doing has given me limitations such as creating a function called "addPopularity(products)" and was wondering how to filter that array in a function, thanks!
@PabloJr.Villasenor well, you'd just wrap the code in my answer in a function. I've updated it with an example
Thanks Phil! Much appreciated for the extra resources youve psoted as well, will have further research into that as well!
1

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

3 Comments

I always appreciate an answer that doesn't mutate the source data +1
I had not noticed the edition, I thought I had done something wrong and I overwritten it, your comment makes sense, I do not know how to apply the edition again
No probs. I usually just open the console and run JSON.stringify(dataFromTheQuestion) and copy / paste it into the answer
1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.