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

我有一个对象数组,如下所示:


    "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 相同,我想将这些对象合并到单个对象中,并将产品信息创建到主数组中的对象数组中


慕无忌1623718
浏览 84回答 2
2回答

慕森王

如果您使用我的库来执行此操作,您将有一段轻松的时光。const data = { orders: [&nbsp; { "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 } = rubicoconst productKeys = ['productID', 'name', 'price', 'image', 'quantity']const addOrderToMap = (m, order) => {&nbsp; if (m.has(order.orderID)) {&nbsp; &nbsp; m.get(order.orderID).products.push(pick(productKeys)(order))&nbsp; } else {&nbsp; &nbsp; m.set(order.orderID, {&nbsp; &nbsp; &nbsp; ...omit(productKeys)(order),&nbsp; &nbsp; &nbsp; products: [pick(productKeys)(order)],&nbsp; &nbsp; })&nbsp; }&nbsp; return m}const groupedByOrderID = assign({&nbsp; orders: pipe([ // assign orders key&nbsp; &nbsp; get('orders'), // data => orders&nbsp; &nbsp; reduce(addOrderToMap, new Map()), // orders => Map { orderID -> orderWithProducts }&nbsp; &nbsp; m => m.values(), // Map { orderID -> orderWithProducts } -> iterator { orderWithProducts }&nbsp; &nbsp; Array.from, // iterator { orderWithProducts } -> [orderWithProducts]&nbsp; ]),})(data)console.log(groupedByOrderID)<script src="https://unpkg.com/rubico/index.js"></script>

米琪卡哇伊

以防万一,如果你想通过平原来做,你可以利用:JavaScriptreducevar 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})=>{&nbsp; &nbsp;acc[rest.orderID] = acc[rest.orderID] || {...rest, products:[]};&nbsp; &nbsp;acc[rest.orderID].products.push({productID, name, price,image, quantity});&nbsp; &nbsp;return acc;},{}));console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript