I have an Array of Objects containing different product names. Each object also contains a rating (1 star, 2 stars, 3 stars, 4 stars, 5 stars).
let myProducts = [
{
productName: "Name of the product",
rating: '5 Stars',
},
{
productName: "Name of the product",
rating: '4 Stars',
},
{
productName: "Name of the product",
rating: '3 Stars',
},
{
productName: "Name of the product",
rating: '5 Stars',
},
{
productName: "Name of the product",
rating: '4 Stars',
},
]
As well as the number of stars shown, I also want to include individual ratings. The user should be able to see each individual rating, the user who wrote that rating and the text included in that rating.
You can add an array to each item with the individual ratings. This architecture can help you:
let myProducts = [
{
productName: "Name of the product",
rating: '5 Stars',
individualRatings: [
{
rating: 5,
user: "John Doe",
text: "I really enjoyed this product!"
},
{
rating: 4,
user: "Jane Doe",
text: "It was ok."
}
]
},
{
productName: "Name of the product",
rating: '4 Stars',
individualRatings: [
{
rating: 4,
user: "John Doe",
text: "I really enjoyed this product!"
},
{
rating: 3,
user: "Jane Doe",
text: "It was ok."
}
]
},
]
Extra: Then you can also generate an average between all the IndividualRatings to get the final rating of the product. Also you can delete the general rating and calculate this in the client side maping rating in IndividualRatings (ex: IndividualRatings[x].rating
)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments