javascript - 对对象数组中的两个值求和,然后插入到另一个对象数组中

标签 javascript arrays object mapping

我有一个名为商店的对象数组:

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
  },
]

我试过映射 stores 数组但卡在了这里:

const storesUpdated = stores.map((store) => {
   = {}
  items.forEach(item => {
    if (item._storeId === store.storeId) {
      return totalCost {
        'storeId' : item.storeId,

      }
    }
  })
})

有什么想法吗?非常感谢。

最佳答案

const 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",},]
const items = [{itemId:1,cost:10,price:20,sold:!1,_storeId:1,},{itemId:2,cost:10,price:20,sold:!1,_storeId:1,},{itemId:3,cost:5,price:12,sold:!0,_storeId:2,},{itemId:4,cost:12,price:20,sold:!1,_storeId:3,},{itemId:5,cost:2,price:10,sold:!1,_storeId:4,},{itemId:6,cost:10,price:50,sold:!0,_storeId:4,},]

const storesUpdated = stores.map((store) => {
  const updatedStore = { ...store,
    totalCost: 0,
    totalPrice: 0,
    countTotalItems: 0,
    countSoldItems: 0
  }
  
  items.forEach(item => {
    if (item._storeId === store.storeId) {
      updatedStore.totalCost += item.cost
      updatedStore.totalPrice += item.price
      updatedStore.countTotalItems += 1
      updatedStore.countSoldItems += item.sold ? 1 : 0
    }
  })
  
  return updatedStore
})

console.log(storesUpdated)

关于javascript - 对对象数组中的两个值求和,然后插入到另一个对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52769684/

相关文章:

javascript - Angular JS "Controller as"语法不起作用

javascript - 用于卡住表格标题的 Chrome css 绝对位置

javascript - 在 JavaScript 中按值从数组中删除项目会产生不可预测的结果

javascript - 如何将对象(及其属性/值)打印到 DOM

javascript - JavaScript 对象中的自引用是否会导致性能不佳?

javascript - 在数组中查找唯一对象

javascript - 将存储在变量中的对象添加到另一个对象而不指定键就是使用变量名作为键。这是预期的行为吗?

javascript - 将 jQuery 搜索限制为特定容器,例如 h 和 p 标签

javascript - 无法根据需要在导航栏中显示项目

Javascript一维到多维数组