在JavaScript中向对象数组添加产品评级。

huangapple go评论67阅读模式
英文:

Add product ratings to an Array of objects JavaScript

问题

我有一个包含不同产品名称的对象数组。每个对象还包含一个评级(1星、2星、3星、4星、5星)。

let myProducts = [
    {
        productName: "产品名称",
        rating: '5星',
    },
    {
        productName: "产品名称",
        rating: '4星',
    },
    {
        productName: "产品名称",
        rating: '3星',
    },
    {
        productName: "产品名称",
        rating: '5星',
    },
    {
        productName: "产品名称",
        rating: '4星',
    },
]

除了显示星级数量,我还希望包括各个评级。用户应该能够看到每个单独的评级,写下该评级的用户以及包含在该评级中的文本。

英文:

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.

答案1

得分: 1

你可以为每个商品添加一个包含个人评分的数组。这种架构可以帮助你:

let myProducts = [
    {
        productName: "产品名称",
        rating: '5 星',
        individualRatings: [
            {
                rating: 5,
                user: "约翰·多",
                text: "我真的很喜欢这个产品!"
            },
            {
                rating: 4,
                user: "简·多",
                text: "还行。"
            }
        ]
    },
    {
        productName: "产品名称",
        rating: '4 星',
        individualRatings: [
            {
                rating: 4,
                user: "约翰·多",
                text: "我真的很喜欢这个产品!"
            },
            {
                rating: 3,
                user: "简·多",
                text: "还行。"
            }
        }
    }
]

额外信息:然后你还可以计算所有个人评分的平均值来获得产品的最终评分。此外,你可以删除总体评分,然后在客户端映射个人评分来计算评分(例如:IndividualRatings[x].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)

huangapple
  • 本文由 发表于 2023年2月19日 05:48:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定