2016-10-18 21 views
-1

私が持っている配列をグループ化したい。たとえば、次のように配列のjavascriptグループ

var shoppingCart = [ 
    { 
     productId: '123', 
     price: 12,99 
     quantity: 1, 
     shopId: 'abc', 
     shopName: 'shop 1' 
    }, 
    { 
     productId: '457', 
     price: 83,33 
     quantity: 2, 
     shopId: 'asw', 
     shopName: 'shop 2' 
    }, 
    { 
     productId: '4232', 
     price: 47,21 
     quantity: 1, 
     shopId: 'abc', 
     shopName: 'shop 1' 
    }, 
    { 
     productId: '3332', 
     price: 9,99 
     quantity: 4, 
     shopId: 'abc', 
     shopName: 'shop 1' 
    }, 
    { 
     productId: '33221', 
     price: 99,21 
     quantity: 1, 
     shopId: 'asw', 
     shopName: 'shop 2' 
    }, 
    { 
     productId: '452113', 
     price: 22,45 
     quantity: 2, 
     shopId: 'asdj', 
     shopName: 'shop 3' 
    } 
] 

私は次のように表示する:

  • ショップ1 のProductId:123 のProductId:4232 のProductId:3332
  • ショップ2 のProductId:457 のProductId:33221
  • ショップ3 プロダクトID 452113

どうすればいいですか?アイデアは、各店舗ごとに別々の注文が作成されることです。

+0

また、フィルタ()メソッドを使用して、あなたはカンマ区切りの番号を持つことができませんJSにその変更を提案します22.45' – evolutionxbox

+0

[コレクション]セクションの[lodash](https://lodash.com/docs/4.16.4)を試してください – vsync

答えて

2

reduceを使用して、固有のショップ名ごとにキーを持つオブジェクトを作成します。あなたが一緒に行くように、これらの配列にアイテムをプッシュ:あなたはこのようなreduce()を使用してオブジェクトを返すことができ

// Group objects in an array by a property 
 
var mapBy = function(arr, groupName, propName) { 
 
    return arr.reduce(function(result, item) { 
 
    result[item[groupName]] = result[item[groupName]] || []; 
 
    result[item[groupName]].push(item[propName]); 
 
    return result; 
 
    }, {}); 
 
}; 
 

 
var shoppingCart=[{productId:"123",price:12.99,quantity:1,shopId:"abc",shopName:"shop 1"},{productId:"457",price:83.33,quantity:2,shopId:"asw",shopName:"shop 2"},{productId:"4232",price:47.21,quantity:1,shopId:"abc",shopName:"shop 1"},{productId:"3332",price:9.99,quantity:4,shopId:"abc",shopName:"shop 1"},{productId:"33221",price:99.21,quantity:1,shopId:"asw",shopName:"shop 2"},{productId:"452113",price:22.45,quantity:2,shopId:"asdj",shopName:"shop 3"}]; 
 

 
console.log(mapBy(shoppingCart, "shopName", "productId"));

1

var shoppingCart = [{"productId":"123","price":12,"quantity":1,"shopId":"abc","shopName":"shop 1"},{"productId":"457","price":83,"quantity":2,"shopId":"asw","shopName":"shop 2"},{"productId":"4232","price":47,"quantity":1,"shopId":"abc","shopName":"shop 1"},{"productId":"3332","price":9,"quantity":4,"shopId":"abc","shopName":"shop 1"},{"productId":"33221","price":99,"quantity":1,"shopId":"asw","shopName":"shop 2"},{"productId":"452113","price":22,"quantity":2,"shopId":"asdj","shopName":"shop 3"}] 
 

 
var r = shoppingCart.reduce(function(r, e) { 
 
    r[e.shopName] = (r[e.shopName] || []).concat({productId: e.productId}) 
 
    return r; 
 
}, {}) 
 

 
console.log(r)

0

私は `に22,45`` ...

var shoppingCart = [ 
{ 
    productId: '123', 
    price: 12.99, 
    quantity: 1, 
    shopId: 'abc', 
    shopName: 'shop 1' 
}, 
{ 
    productId: '457', 
    price: 83.33, 
    quantity: 2, 
    shopId: 'asw', 
    shopName: 'shop 2' 
}, 
{ 
    productId: '4232', 
    price: 47.21, 
    quantity: 1, 
    shopId: 'abc', 
    shopName: 'shop 1' 
}, 
{ 
    productId: '3332', 
    price: 9.99, 
    quantity: 4, 
    shopId: 'abc', 
    shopName: 'shop 1' 
}, 
{ 
    productId: '33221', 
    price: 99.21, 
    quantity: 1, 
    shopId: 'asw', 
    shopName: 'shop 2' 
}, 
{ 
    productId: '452113', 
    price: 22.45, 
    quantity: 2, 
    shopId: 'asdj', 
    shopName: 'shop 3' 
} 
]; 
var shop1 = shoppingCart.filter(x => x.shopName === 'shop 1').map(x => ({productId: x.productId})); 

console.log(shop1); // [ { productId: '123' }, { productId: '4232' },{ productId: '3332' } ] 
関連する問題