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

Javascript 创建对象的新排列并对重复对象分组,javascript,arrays,lodash,Javascript,Arrays,Lodash,我有一个像这样的对象数组 [ { 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-0

我有一个像这样的对象数组

[
 { 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" }, },
];
[
 {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,但我不能提高很多,因为我不理解它,我使用grouby和lodash,但我不能提高更多

我需要它保持这样

[
 { 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" }, },
];
[
 {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,
注:“测试”,
成员编号:1,
更新地址:“2021-04-08T15:59:08.210754”,
产品编号:1,
产品:{
id:1,
名称:“测试”,
},
},
{
id:2,
注:“,
成员编号:1,
更新地址:“2021-04-08T18:07:19.712304”,
产品编号:5,
产品:{
id:5,
名称:“保险”
},
},
{
id:3,
注:“一注”,
成员编号:1,
更新地址:“2021-04-08T18:11:26.444726”,
产品编号:5,
产品:{
id:5,
名称:“保险”
},
},
{
id:4,
注:“萨达德”,
成员编号:1,
更新地址:“2021-04-08T18:28:59.8435”,
产品编号:5,
产品:{
id:5,
名称:“保险”
},
},
];
常量结果=列表减少((acc,x)=>{
const index=acc.findIndex(obj=>obj.product==x.product.name);
常数salesObj={
id:x.id,
注:十、注:,
成员id:x.成员id,
更新时间:x。更新时间:,
产品标识:x.产品标识
};
如果(指数<0){
acc=[…acc{
产品:x.product.name,
数量:1,
更新时间:x。更新时间:,
销售:[salesObj]
}];
返回acc;
}
acc[索引]。更新的位置=x。更新的位置;
acc[索引]。数量++;
acc[index].sales.push(salesObj);
返回acc;
}, []);

console.log(result)
您可以尝试类似的方法,它将为您提供所需的输出

const list=[{
id:1,
注:“测试”,
成员编号:1,
更新地址:“2021-04-08T15:59:08.210754”,
产品编号:1,
产品:{
id:1,
名称:“测试”,
},
},
{
id:2,
注:“,
成员编号:1,
更新地址:“2021-04-08T18:07:19.712304”,
产品编号:5,
产品:{
id:5,
名称:“保险”
},
},
{
id:3,
注:“一注”,
成员编号:1,
更新地址:“2021-04-08T18:11:26.444726”,
产品编号:5,
产品:{
id:5,
名称:“保险”
},
},
{
id:4,
注:“萨达德”,
成员编号:1,
更新地址:“2021-04-08T18:28:59.8435”,
产品编号:5,
产品:{
id:5,
名称:“保险”
},
},
];
常量结果=列表减少((acc,x)=>{
const index=acc.findIndex(obj=>obj.product==x.product.name);
常数salesObj={
id:x.id,
注:十、注:,
成员id:x.成员id,
更新时间:x。更新时间:,
产品标识:x.产品标识
};
如果(指数<0){
acc=[…acc{
产品:x.product.name,
数量:1,
更新时间:x。更新时间:,
销售:[salesObj]
}];
返回acc;
}
acc[索引]。更新的位置=x。更新的位置;
acc[索引]。数量++;
acc[index].sales.push(salesObj);
返回acc;
}, []);

console.log(result)
使用
.gropyBy
对项目进行分组,使用
.toPairs
获取对象的键值对,然后使用
映射
自定义组中的对象

这里的
列表
是输入数组

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)

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

这里的
列表
是输入数组

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)

这会给你想要的结果


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

也许这会对你有所帮助。您尚未显示任何尝试,请提供。另外,请提供您想要的最终JSON结构。我已经完成了丢失的数据,您能指导我如何解决这个问题吗@尤金:这能回答你的问题吗?也许这会对你有所帮助。您尚未显示任何尝试,请提供。另外,请提供您想要的最终JSON结构。我已经完成了丢失的数据,您能指导我如何解决这个问题吗@尤金:这能回答你的问题吗?这就是我一直在寻找的答案,我想问你我如何在新对象中放置盐的上次更新?哪个新对象?{“产品”:“保险”,“数量”:3,“更新位置”:“2021-04-08”,sales:[{…}]}@JorgeBarcos更新然后现有对象获得最新值/更新,如
acc[index].updated_at=x.updated_at添加了更改,请查看这是我一直在寻找的答案,我想问你我如何在新对象中添加盐的最后更新?哪个新对象?{“产品”:“保险”,“数量”:3,“更新位置”:“2021-04-08”,销售:[{…}}@JorgeBarcos然后更新现有对象以获取最新值/更新,如下所示
acc[index].updated_at=x.updated_at已添加更改,请签出