J'ai des données d'objet comme suit:

[
    {
        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"   
        }      
    }
]

Je souhaite donc regrouper les données par "seller_id" et fusionner les données de niveau supérieur affectées au nom d'objet "commandes", comme suit:

[
    {
        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
            }
        ]
    }
]

J'ai essayé de résoudre ce problème il y a plusieurs heures. Quelqu'un peut-il résoudre ce cas?

Je vous remercie

0
EroNeko 27 janv. 2019 à 18:46

3 réponses

Meilleure réponse

Vous pouvez utiliser _.groupBy() puis _.map() les groupes au format demandé:

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>
0
Ori Drori 27 janv. 2019 à 15:58

Si vous aimez faire avec JS ordinaire, vous pouvez utiliser réduire

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))
0
Code Maniac 27 janv. 2019 à 16:05

Voici une solution JS pure utilisant réduire :

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);
0
jo_va 27 janv. 2019 à 16:14