У меня есть массив объектов, называемых магазинами:
stores = [
{
storeId: 1,
city: "San Francisco",
state: "CA",
},
{
storeId: 2,
city: "Seattle",
state: "WA",
},
{
storeId: 3,
city: "Vancouver",
state: "BC",
},
{
storeId: 4,
city: "Los Angeles",
state: "CA",
},
]
и другой массив объектов, называемых элементами:
items = [
{
itemId: 1,
cost: 10,
price: 20,
sold: false,
_storeId: 1,
},
{
itemId: 2,
cost: 10,
price: 20,
sold: false,
_storeId: 1,
},
{
itemId: 3,
cost: 5,
price: 12,
sold: true,
_storeId: 2,
},
{
itemId: 4,
cost: 12,
price: 20,
sold: false,
_storeId: 3,
},
{
itemId: 5,
cost: 2,
price: 10,
sold: false,
_storeId: 4,
},
{
itemId: 6,
cost: 10,
price: 50,
sold: true,
_storeId: 4,
},
]
Я хочу суммировать следующие категории по магазинам:
Затем подсчитайте общее количество товаров по магазину:
Затем подсчитайте промежуточные товары, проданные в магазине:
, поэтому мой окончательный массив магазина выглядит примерно так:
storesUpdated = [
{
storeId: 1,
city: "San Francisco",
state: "CA",
totalCost: 20,
totalPrice: 40,
countTotalItems: 2,
countSoldItems: 0
},
{
storeId: 2,
city: "Seattle",
state: "WA",
totalCost: 5,
totalPrice: 12,
countTotalItems: 1,
countSoldItems: 1
},
{
storeId: 3,
city: "Vancouver",
state: "BC",
totalCost: 12,
totalPrice: 20,
countTotalItems: 1,
countSoldItems: 0
},
{
storeId: 4,
city: "Los Angeles",
state: "CA",
totalCost: 12,
totalPrice: 60,
countTotalItems: 2,
countSoldItems: 1
},
]
I 'мы пробовали отображать массив магазинов, но застряли здесь:
const storesUpdated = stores.map((store) => {
= {}
items.forEach(item => {
if (item._storeId === store.storeId) {
return totalCost {
'storeId' : item.storeId,
}
}
})
})
Есть идеи?Большое спасибо.