创建新的对象排列并将重复对象分组

Create a new arrangement of objects and group repeating objects

我有一组像这样的对象

[
 { id: 1, note: "TEST", member_id: 1, updated_at: "2021-04-08T15:59:08.210754", product_id: 1,
 product: { id: 1, name: "Testing",},},
 {id: 2, note: "", member_id: 1, updated_at: "2021-04-08T18:07:19.712304", product_id: 5,
 product: { id: 5, name: "Insurance"}, },
 { id: 3, note: "One note", member_id: 1, updated_at: "2021-04-08T18:11:26.444726", product_id: 5, 
 product: { id: 5, name: "Insurance" },},
{ id: 4, note: "sadad", member_id: 1, updated_at: "2021-04-08T18:28:59.8435", product_id: 5, product: 
 { id: 5, name: "Insurance" }, },
];

我必须在 table 中表示它,它可以按产品名称分组,计算具有相同产品的对象的数量,并显示其中任何对象的最后更新。

它应该是这样的

尝试使用 reduce 但我不能进步太多,因为我不理解它,我使用 grouby 和 lodash 但我不能进步更多

我需要它保持这样

[
 {product: "Insurance", 
 quantity: 3, 
 updated_at: "2021-04-08", 
 sales: [ 
         { id: 2, 
           note: "", 
           member_id: 1, 
           updated_at: "2021-04-08", 
           product_id: 5
         }, 
         { id: 3, 
           note: "One note", 
           member_id: 1, 
           updated_at: "2021-04-08", 
           product_id: 5
         }, 
         { id: 4, 
           note: "sadad", 
           member_id: 1, 
           updated_at: "2021-04-08",
           product_id: 5
         }
        ]
  },
  {product: "Testing", 
   quantity: 1, 
   updated_at: "2021-04-08", 
   sales: [ 
           {id: 1, 
            note: "TEST", 
            member_id: 1, 
            updated_at: "2021-04-08", 
            product_id: 1,} 
          ]},
]

我尝试使用 reduce 但它不是我所期望的,我也不太了解它

array.reduce((acc, it) => {
acc[it.product.name] = acc[it.product.name] + 1 || 1;
return acc;
}, {});

您可以尝试这样的操作,它应该会为您提供所需的输出。

const list = [{
    id: 1,
    note: "TEST",
    member_id: 1,
    updated_at: "2021-04-08T15:59:08.210754",
    product_id: 1,
    product: {
      id: 1,
      name: "Testing",
    },
  },
  {
    id: 2,
    note: "",
    member_id: 1,
    updated_at: "2021-04-08T18:07:19.712304",
    product_id: 5,
    product: {
      id: 5,
      name: "Insurance"
    },
  },
  {
    id: 3,
    note: "One note",
    member_id: 1,
    updated_at: "2021-04-08T18:11:26.444726",
    product_id: 5,
    product: {
      id: 5,
      name: "Insurance"
    },
  },
  {
    id: 4,
    note: "sadad",
    member_id: 1,
    updated_at: "2021-04-08T18:28:59.8435",
    product_id: 5,
    product: {
      id: 5,
      name: "Insurance"
    },
  },
];

const result = list.reduce((acc, x) => {
  const index = acc.findIndex(obj => obj.product === x.product.name);
  const salesObj = {
    id: x.id,
    note: x.note,
    member_id: x.member_id,
    updated_at: x.updated_at,
    product_id: x.product_id
  };
  if (index < 0) {
    acc = [...acc, {
      product: x.product.name,
      quantity: 1,
      updated_at: x.updated_at,
      sales: [salesObj]
    }];
    return acc;
  }
  acc[index].updated_at = x.updated_at;
  acc[index].quantity++;
  acc[index].sales.push(salesObj);
  return acc;
}, []);

console.log(result)

使用_.gropyBy对项目进行分组,使用_.toPairs获取对象的键值对,然后使用map自定义组中的对象

这里list是输入数组

var result = _.toPairs(_.groupBy(list, 'product.name'))
 .map(([product, sales]) => {
    const quantity = sales.length;
        return {
            product, 
            quantity, 
            updated_at: sales[quantity-1].updated_at, 
            sales: sales.map(item => _.omit(item, 'product')) 
        }
    });
console.log(result)
const sales = [
  {
    "id": 1,
    "note": "TEST",
    "member_id": 1,
    "updated_at": "2021-04-08T15:59:08.210754",
    "product_id": 1,
    "product": {
      "id": 1,
      "name": "Testing"
    }
  },
  {
    "id": 2,
    "note": "",
    "member_id": 1,
    "updated_at": "2021-04-08T18:07:19.712304",
    "product_id": 5,
    "product": {
      "id": 5,
      "name": "Insurance"
    }
  },
  {
    "id": 3,
    "note": "One note",
    "member_id": 1,
    "updated_at": "2021-04-08T18:11:26.444726",
    "product_id": 5,
    "product": {
      "id": 5,
      "name": "Insurance"
    }
  },
  {
    "id": 4,
    "note": "sadad",
    "member_id": 1,
    "updated_at": "2021-04-08T18:28:59.8435",
    "product_id": 5,
    "product": {
      "id": 5,
      "name": "Insurance"
    }
  }
];


const newObject = sales.reduce((allProducts, singleSale) => {
  if(allProducts.has(singleSale.product.name)){

    const product = allProducts.get(singleSale.product.name);
    
    allProducts.set(singleSale.product.name, {
      ...product,
      quantity: product.sales.length + 1,
      sales: [...product.sales, singleSale]
    });

  } else {
    allProducts.set(singleSale.product.name, {
      product: singleSale.product.name,
      quantity: 1, 
      sales: [singleSale]
    })
  }
  
  return allProducts;
}, new Map());

console.log([...newObject.values()])

https://jsbin.com/govocoyeri/edit?js,console

这应该会给你想要的结果。