Add product ratings to an Array of objects JavaScript

user21194080

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.

Juan Melnechuk

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.

edited at
0

Comments

0 comments
Login to comment

Related

Cartesian product of array of objects in javascript

Add a product review with ratings programmatically in Woocommerce

Add an array to array of objects in javascript

Add property to an array of objects in JavaScript

Add id to array of objects - Javascript

Javascript Add a list of objects into existing Array

How to add a value in array of JSON objects on javascript?

add object to another array of objects in javascript

How to add non duplicate objects in an array in javascript?

JavaScript: Add up numbers in nested array of objects

Insert / add / push array of an objects into Object in Javascript

Failing to add objects to an empty array in javascript

JavaScript add arrays to existing array of objects

Javascript - How to add multiple objects into an empty array

How to group and add their value in array of objects in javascript?

Using JavaScript reduce to update and add to array of objects

Calculate average of ratings in array, then add field to original document in MongoDB

Javascript dynamically add new Array Objects into existing Array (with keys)?

JavaScript: Add JSON objects from one array to another array conditionally

Unable to add second array object value into first array of objects in javascript

Javascript game Create new objects automatically to add into an array continuously [javascript]

Add product with array of options to cart

Add objects dinamically to an array of objects

How to add title/key to an array of multiple objects in javascript?

Remove duplicate objects in an array but keep add together a few properties in javascript

Add dynamic array objects from filtered data in Javascript - Typescript

Add a new property to an object from an array of objects in Javascript

How to add numbers from items in an array of objects in Javascript to output to a table

Add JSON object from array of objects into another complex array of objects using JavaScript in efficient way