使用Lodash分组和合并嵌套对象

时间:2019-01-27 15:46:11

标签: javascript object multidimensional-array lodash

我的对象数据如下:

[
    {
        name: "Green Tea Brownie",
        price: 60,
        amount: 10,
        seller: {
            seller_id: 124,
            seller_name: "Merry Shop"
        }      
    },
    {
        name: "Cocoa Chiffon",
        price: 20,
        amount: 50,
        seller: {
            seller_id: 124,
            seller_name: "Merry Shop"          
        }      
    },
    {
        name: "Milky Donut",
        price: 40,
        amount: 100
        seller: {
            seller_id: 421,
            seller_name: "Sweet Bakery"   
        }      
    }
]

所以我想按“ seller_id”对数据进行分组并合并分配给对象名称“ orders”的顶级数据,如下所示:

[
    {
        seller_id: 124,
        seller_name: "Merry Shop",
        orders: [
            {
                name: "Green Tea Brownie",
                price: 60,
                amount: 10
            },
            {
                name: "Cocoa Chiffon",
                price: 20,
                amount: 50
            }
        ]
    },
    {
        seller_id: 421,
        seller_name: "Sweet Bakery",
        orders: [
            {
                name: "Milky Donut",
                price: 40,
                amount: 100
            }
        ]
    }
]

几个小时前,我试图解决此问题。谁能解决这个问题?

谢谢

3 个答案:

答案 0 :(得分:0)

您可以先使用_.groupBy(),然后再使用_.map()的组以请求的格式:

const { flow, partialRight: pr, groupBy, map, first, omit } = _

const fn = flow(
  pr(groupBy, 'seller.seller_id'),
  pr(map, group => ({
    ...first(group).seller,
    orders: map(group, pr(omit, 'seller'))
  }))
)

const data = [{"name":"Green Tea Brownie","price":60,"amount":10,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Cocoa Chiffon","price":20,"amount":50,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Milky Donut","price":40,"amount":100,"seller":{"seller_id":421,"seller_name":"Sweet Bakery"}}]

const result = fn(data)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

答案 1 :(得分:0)

如果您想使用普通JS,可以使用reduce

const data = [{"name":"Green Tea Brownie","price":60,"amount":10,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Cocoa Chiffon","price":20,"amount":50,"seller":{"seller_id":124,"seller_name":"Merry Shop"}},{"name":"Milky Donut","price":40,"amount":100,"seller":{"seller_id":421,"seller_name":"Sweet Bakery"}}]


const output = data.reduce((op,{name,price,amount,seller})=>{
  if(op[seller['seller_id']]){
    op[seller['seller_id']]['orders'].push({name,price,amount})
   }
  else {
    op[seller['seller_id']] = {
      seller_name: seller.seller_name,
      seller_id: seller.seller_id,
      orders: [{name,price,amount}]
    }
  }
 return op;
},{})

console.log(Object.values(output))

答案 2 :(得分:0)

这是使用 reduce 的纯JS解决方案:

const orders = [{
    name: "Green Tea Brownie",
    price: 60,
    amount: 10,
    seller: {
        seller_id: 124,
        seller_name: "Merry Shop"
    }      
}, {
    name: "Cocoa Chiffon",
    price: 20,
    amount: 50,
    seller: {
        seller_id: 124,
        seller_name: "Merry Shop"          
    }      
}, {
    name: "Milky Donut",
    price: 40,
    amount: 100,
    seller: {
        seller_id: 421,
        seller_name: "Sweet Bakery"   
    }      
}];

const ordersBySeller = Object.values(orders.reduce((accum, { name, price, amount, seller: { seller_id, seller_name } }) => {
  const sellerOrders = accum[seller_id] ? accum[seller_id].orders || [] : [];
  accum[seller_id] = { seller_id, seller_name, orders: [...sellerOrders, { name, price, amount } ] };
  return accum;
}, {}));

console.log(ordersBySeller);