two array one in price and productname and the second is username, quantity, productname how to group this two array in one by username using pure js?

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

two array one in price and productname and the second is username, quantity, productname how to group this two array in one by username using pure js?

问题

这是我的数组:

let prices = [
    {
        product: "p1",
        price: 100,
    },
    {
        product: "p2",
        price: 120.2,
    },
    {
        product: "p3",
        price: 120,
    },
    {
        product: "p4",
        price: 125,
    }
]

let orders = [
    {
        user: "user1",
        product: "p1",
        quantity: 2,
    },
    {
        user: "user1",
        product: "p1",
        quantity: 4,
    },
    {
        user: "user4",
        product: "p3",
        quantity: 5,
    },
    {
        user: "user4",
        product: "p4",
        quantity: 10,
    },
    {
        user: "user3",
        product: "p2",
        quantity: 10,
    },
    {
        user: "user4",
        product: "p1",
        quantity: 10,
    },
    {
        user: "user1",
        product: "p2",
        quantity: 10,
    },
    {
        user: "user3",
        product: "p2",
        quantity: 20,
    }
]

如何按用户分组以获得以下结果:

result = [
    {
        user: "user1",
        product: [
            {
                productName: "p1",
                quantity: 6,
                itemPrice: 100,
                totalPrice: 600
            }
        ],
        total: 600
    },
    {
        user: "user4",
        product: [
            {
                productName: "p3",
                quantity: 5,
                itemPrice: 120,
                totalPrice: 600
            },
            {
                productName: "p4",
                quantity: 10,
                itemPrice: 125,
                totalPrice: 1250
            }
        ],
        total: 1850
    }
]
英文:

Here's my arrays:

let prices = [
    {
        product: "p1",
        price: 100,
    },
    {
        product: "p2",
        price: 120.2,
    },
    {
        product: "p3",
        price: 120,
    },
    {
        product: "p4",
        price: 125,
    },
]

let orders = [
    {
        user: "user1",
        product: "p1",
        quantity: 2,
    },
    {
        user: "user1",
        product: "p1",
        quantity: 4,
    },
    {
        user: "user4",
        product: "p3",
        quantity: 5,
    },
    {
        user: "user4",
        product: "p4",
        quantity: 10,
    },
    {
        user: "user3",
        product: "p2",
        quantity: 10,
    },
    {
        user: "user4",
        product: "p1",
        quantity: 10,
    },
    {
        user: "user1",
        product: "p2",
        quantity: 10,
    },
    {
        user: "user3",
        product: "p2",
        quantity: 20,
    }
]

How can I group it by user so that I would get the result of:

result = [
    {
        user: "user1",
        product: [
            {
                productName: "p1",
                quantity: 6,
                itemPrice: 100,
                totalPrice: 600
            }
        ],
        total: 600
    },
    {
        user: "user4",
        product: [
            {
                productName: "p3",
                quantity: 5,
                itemprice: 120,
                totalPrice: 600
            },
            {
                productName: 'p4',
                quantity: 10,
                itemprice: 125,
                totalPrice: 1250
            }
        ],
        total: 1850
    }
]

答案1

得分: 1

你可以首先找到客户组,然后找到商品,并创建价格和数量的产品。

为了更容易访问价格,你可以使用一个名为 pricesByProduct 的对象。

const
    prices = [{ product: "p1", price: 100 }, { product: "p2", price: 120.2 }, { product: "p3", price: 120 }, { product: "p4", price: 125 }],
    orders = [{ user: "user1", product: "p1", quantity: 2 }, { user: "user1", product: "p1", quantity: 4 }, { user: "user4", product: "p3", quantity: 5 }, { user: "user4", product: "p4", quantity: 10 }, { user: "user3", product: "p2", quantity: 10 }, { user: "user4", product: "p1", quantity: 10 }, { user: "user1", product: "p2", quantity: 10 }, { user: "user3", product: "p2", quantity: 20 }],
    pricesByProduct = Object.fromEntries(prices.map(({ product, price }) => [product, price])),
    result = orders.reduce((r, { user, product: productName, quantity }) => {
        let customer = r.find(q => q.user === user);
        if (!customer) r.push(customer = { user, product: [], total: 0 });

        let item = customer.product.find(q => q.productName === productName);
        if (!item) customer.product.push(item = { productName, quantity: 0, itemPrice: pricesByProduct[productName], totalPrice: 0 });

        let orderPrice = item.itemPrice * quantity;
        item.quantity += quantity;
        item.totalPrice += orderPrice;
        customer.total += orderPrice;

        return r;
    }, []);

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

You could find the group of customer first and then find the item and create the product of price and quantity.

For easier access of prices, you could use an object pricesByProduct.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
prices = [{ product: "p1", price: 100 }, { product: "p2", price: 120.2 }, { product: "p3", price: 120 }, { product: "p4", price: 125 }],
orders = [{ user: "user1", product: "p1", quantity: 2 }, { user: "user1", product: "p1", quantity: 4 }, { user: "user4", product: "p3", quantity: 5 }, { user: "user4", product: "p4", quantity: 10 }, { user: "user3", product: "p2", quantity: 10 }, { user: "user4", product: "p1", quantity: 10 }, { user: "user1", product: "p2", quantity: 10 }, { user: "user3", product: "p2", quantity: 20 }],
pricesByProduct = Object.fromEntries(prices.map(({ product, price }) => [product, price])),
result = orders.reduce((r, { user, product: productName, quantity }) => {
let customer = r.find(q => q.user === user);
if (!customer) r.push(customer = { user, product: [], total: 0 });

        let item = customer.product.find(q =&gt; q.productName === productName);
if (!item) customer.product.push(item = { productName, quantity: 0, itemPrice: pricesByProduct[productName], totalPrice: 0 });
let orderPrice = item.itemPrice * quantity;
item.quantity += quantity;
item.totalPrice += orderPrice;
customer.total += orderPrice;
return r;
}, []);
console.log(result);

<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月7日 15:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659115.html
匿名

发表评论

匿名网友

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

确定