为了账号安全,请及时绑定邮箱和手机立即绑定

比较同一数组的对象,并在特定属性与其他对象匹配时对它们的某些属性进行分组

比较同一数组的对象,并在特定属性与其他对象匹配时对它们的某些属性进行分组

慕无忌1623718 2022-09-29 10:47:42
我有一个对象数组,如下所示:    "orders": [        {            "orderID": 1,            "fullName": "xyz",            "email": "xyz@gmail.com",            "phone": "12345",            "flatNo": "A-5",            "complex": "tara tra",            "landmark": null,            "street": null,            "area": "",            "city": "",            "productID": 2,            "name": "curd",            "price": 52,            "image": "curd.png",            "quantity": 1        },        {            "orderID": 1,            "fullName": "xyz",            "email": "xyz@gmail.com",            "phone": "12345",            "flatNo": "A-5",            "complex": "tara tra",            "landmark": null,            "street": null,            "area": "",            "city": "",            "productID": 1,            "name": "lassi",            "price": 65,            "image": "images\\rtoRAOwj4-conn.PNG",            "quantity": 1        },        {            "orderID": 2,            "fullName": "velocity",            "email": "velocity@gmail.com",            "phone": "999999",            "flatNo": "b-863",            "complex": "tara tra",            "landmark": "kaskd",            "street": "asdasd",            "area": "rob city",            "city": "asda",            "productID": 1,            "name": "lassi",            "price": 65,            "image": "images\\rtoRAOwj4-conn.PNG",            "quantity": 3        }    ]在这里,如果对象的 orderID 相同,我想将这些对象合并到单个对象中,并将产品信息创建到主数组中的对象数组中
查看完整描述

2 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

如果您使用我的库来执行此操作,您将有一段轻松的时光。


const data = { orders: [  { "orderID": 1, "fullName": "xyz", "email": "xyz@gmail.com", "phone": "12345", "flatNo": "A-5", "complex": "tara tra", "landmark": null, "street": null, "area": "", "city": "", "productID": 2, "name": "curd", "price": 52, "image": "curd.png", "quantity": 1 }, { "orderID": 1, "fullName": "xyz", "email": "xyz@gmail.com", "phone": "12345", "flatNo": "A-5", "complex": "tara tra", "landmark": null, "street": null, "area": "", "city": "", "productID": 1, "name": "lassi", "price": 65, "image": "images\\rtoRAOwj4-conn.PNG", "quantity": 1 }, { "orderID": 2, "fullName": "velocity", "email": "velocity@gmail.com", "phone": "999999", "flatNo": "b-863", "complex": "tara tra", "landmark": "kaskd", "street": "asdasd", "area": "rob city", "city": "asda", "productID": 1, "name": "lassi", "price": 65, "image": "images\\rtoRAOwj4-conn.PNG", "quantity": 3 } ] }


const { pipe, assign, reduce, get, pick, omit } = rubico


const productKeys = ['productID', 'name', 'price', 'image', 'quantity']


const addOrderToMap = (m, order) => {

  if (m.has(order.orderID)) {

    m.get(order.orderID).products.push(pick(productKeys)(order))

  } else {

    m.set(order.orderID, {

      ...omit(productKeys)(order),

      products: [pick(productKeys)(order)],

    })

  }

  return m

}


const groupedByOrderID = assign({

  orders: pipe([ // assign orders key

    get('orders'), // data => orders

    reduce(addOrderToMap, new Map()), // orders => Map { orderID -> orderWithProducts }

    m => m.values(), // Map { orderID -> orderWithProducts } -> iterator { orderWithProducts }

    Array.from, // iterator { orderWithProducts } -> [orderWithProducts]

  ]),

})(data)


console.log(groupedByOrderID)

<script src="https://unpkg.com/rubico/index.js"></script>


查看完整回答
反对 回复 2022-09-29
?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

以防万一,如果你想通过平原来做,你可以利用:JavaScriptreduce


var data=[ { "orderID": 1, "fullName": "xyz", "email": "xyz@gmail.com", "phone": "12345", "flatNo": "A-5", "complex": "tara tra", "landmark": null, "street": null, "area": "", "city": "", "productID": 2, "name": "curd", "price": 52, "image": "curd.png", "quantity": 1 }, { "orderID": 1, "fullName": "xyz", "email": "xyz@gmail.com", "phone": "12345", "flatNo": "A-5", "complex": "tara tra", "landmark": null, "street": null, "area": "", "city": "", "productID": 1, "name": "lassi", "price": 65, "image": "images\\rtoRAOwj4-conn.PNG", "quantity": 1 }, { "orderID": 2, "fullName": "velocity", "email": "velocity@gmail.com", "phone": "999999", "flatNo": "b-863", "complex": "tara tra", "landmark": "kaskd", "street": "asdasd", "area": "rob city", "city": "asda", "productID": 1, "name": "lassi", "price": 65, "image": "images\\rtoRAOwj4-conn.PNG", "quantity": 3 } ];


var result = Object.values(data.reduce((acc, {productID, name, price,image, quantity, ...rest})=>{

   acc[rest.orderID] = acc[rest.orderID] || {...rest, products:[]};

   acc[rest.orderID].products.push({productID, name, price,image, quantity});

   return acc;

},{}));


console.log(result);


查看完整回答
反对 回复 2022-09-29
  • 2 回答
  • 0 关注
  • 83 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信