javascript group by in array

时间:2016-10-18 13:55:04

标签: javascript arrays

我想对我拥有的数组进行分组。例如:

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 产品编号:457 ProductId:33221
  • 商店3 ProductId 452113

我该怎么做?我们的想法是为每家商店创建单独的订单。

3 个答案:

答案 0 :(得分:2)

使用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 :(得分:1)

您可以像这样使用reduce()并返回对象。



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)




答案 2 :(得分:0)

我建议使用filter()方法

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' } ]