显示每个密钥的百分比

时间:2018-06-11 09:43:10

标签: javascript arrays object

我想计算对象中每个键的百分比。 我已经计算了每个商店(水果+蔬菜)的总数。而且我想显示每个商店中每种水果或蔬菜的百分比,而不是它们的价值。

百分比=每个商店的水果或蔬菜/每个商店的水果或蔬菜总数

在这一行中,我想要这样的事情:

k(v).forEach(k => k.includes('item') &&
                (s[(i = items[v[k]]).group] += (i.score/total)*100)), o), {});

这是我的代码和数组

  var myArray = [{
                    shop: "shop1",
                    item1: "my apple 1",
                    item2: "my carrot 1"
                },
                {
                    shop: "shop1",
                    item1: "my apple 1",
                    item2: "my carrot 1"
                },
                                                        {
                    shop: "shop2",
                    item1: "my apple 0",
                    item2: "my carrot 0"
                },
                {
                    shop: "shop2",
                    item1: "my apple 0",
                    item2: "my carrot 1"
                }
            ];

            var MyArrayDefinition = [{
                    item: "my apple 0",
                    color: "red",
                    group: "fruit",
                    score: 0
                },
                {
                    item: "my carrot 1",
                    color: "orange",
                    group: "vegetable",
                    score: 1
                },
                {
                    item: "my apple 1",
                    color: "red",
                    group: "fruit",
                    score: 1
                },
                {
                    item: "my carrot 0",
                    color: "orange",
                    group: "vegetable",
                    score: 0
                }
            ];

        var k = Object.keys,
            items = MyArrayDefinition.reduce((o, v) => (o[v.item] = v, o), {});

        var shops = myArray.reduce((o, v, i, s) => (
            s = v[k(v).find(k => k)],
            s = o[s] || (o[s] = {
                fruit: 0,
                vegetable: 0,
                        }),
            k(v).forEach(k => k.includes('item') &&
                (s[(i = items[v[k]]).group] += i.score)), o), {});

        var result = k(shops).map((k, i) => ({
            id: i + 1,
            shop: k,
            itemGroup: shops[k],
            total:Object.values(shops[k]).reduce((a, b) => a + b),
        }));

我喜欢这个结果

 [ { "id": 1, "shop": "shop1", "fruit": 50, "vegetable": 50 , "total": 4 }, { "id": 2, "shop": "shop2" "fruit": 0, "vegetable": 100 , "total": 1 } ]

1 个答案:

答案 0 :(得分:1)

您可以拍摄地图并收集所有计数,然后构建所需的百分比值。

var array = [{ shop: "shop1", item1: "my apple 1", item2: "my carrot 1" }, { shop: "shop1", item1: "my apple 1", item2: "my carrot 1" }, { shop: "shop2", item1: "my apple 0", item2: "my carrot 0" }, { shop: "shop2", item1: "my apple 0", item2: "my carrot 1" }],
    definitions = [{ item: "my apple 0", color: "red", group: "fruit", score: 0 }, { item: "my carrot 1", color: "orange", group: "vegetable", score: 1 }, { item: "my apple 1", color: "red", group: "fruit", score: 1 }, { item: "my carrot 0", color: "orange", group: "vegetable", score: 0 }],
    groups = ['fruit', 'vegetable'],
    items = Object.assign(...definitions.map(({ item, group, score }) => ({ [item]: { group, score } }))),
    result = Array.from(
        array.reduce((map, o) => {
            var shop = map.get(o.shop);
            if (!shop) {
                shop = Object.assign({ itemGroup: {}, total: 0 }, ...groups.map(k => ({ [k]: 0 })));
                map.set(o.shop, shop);
            }
            Object.entries(o).forEach(([k, item]) => {
                if (k === 'shop') {
                    return;
                }
                shop.total += items[item].score;
                shop[items[item].group] += items[item].score;
            });
            groups.forEach(k => shop.itemGroup[k] = shop[k] * 100 / shop.total);
            return map;
        }, new Map),
        ([shop, { itemGroup, total }], i) => (Object.assign({ id: i + 1, shop, total }, itemGroup))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }