Javascript JS:对象的聚合数组

Javascript JS:对象的聚合数组,javascript,arrays,logic,Javascript,Arrays,Logic,我正在处理一系列更改日志(对象) 数组中的每个元素都是项的一个字段更改。 我的数组看起来像这样 changeLogArray=[{"ItemNo":"01", "Field":"Price","OldValue":"100","NewValue":"200","CreatedDate":"17/10/2020

我正在处理一系列更改日志(对象)

数组中的每个元素都是项的一个字段更改。 我的数组看起来像这样

changeLogArray=[{"ItemNo":"01", "Field":"Price","OldValue":"100","NewValue":"200","CreatedDate":"17/10/2020"},
{"ItemNo":"01", "Field":"Price","OldValue":"200","NewValue":"300","CreatedDate":"18/10/2020"},
{"ItemNo":"01", "Field":"Price","OldValue":"300","NewValue":"400","CreatedDate":"19/10/2020"},
{"ItemNo":"01", "Field":"Name","OldValue":"A","NewValue":"B","CreatedDate":"19/10/2020"}]
我想把字段价格的唯一变化合并成一行 我想要的结果: 字段更改仅一行=价格、第一条记录的旧值、最后一条记录的新值(orderby createdDate)

这就是我现在拥有的

                            var lstToDisplayNotSellingPrice = changeLogArray.filter(item => {
                            return item.Field != 'Selling Price'
                        })

                        var lstToDisplaySellingPrice  = changeLogArray.filter(item => {
                            return item.Field == 'Selling Price'
                        })

                        var resultChangeLogSellingPrice = []


                        changeLogArray.forEach((item, index) =>{
                            
                            var distinctItemIndex
                            if(index == 0 || item.ItemNo != lstToDisplaySellingPrice[index-1].ItemNo){
                                resultChangeLogSellingPrice.push(item)
                                distinctItemIndex = index
                            }else{
                                if(item.FieldLevel == lstToDisplaySellingPrice[index-1].ItemNo){
                                    resultChangeLogSellingPrice.pop()
                                    var itemLog = lstToDisplaySellingPrice[index-1]
                                    itemLog.NewValue = item.NewValue
                                    itemLog.CreatedDate = item.CreatedDate
                                    resultChangeLogSellingPrice.push(itemLog)
                                }
                            }

                        });
我尝试先将字段=销售价格的字段分开,然后使用仅包含销售价格变化的数组,并对每个ItemNo使用forEach如果当前行的ItemNo与上一行的ItemNo相同,则弹出上一个日志,然后推送具有当前行的NewValue和CreatedDate的新日志。
最后,我将把价格变化列表和其他变化列表合并到结果数组中

通过使用
CreatedDate
的排序项目数组,您可以使用
ItemNo
字段
对对象进行分组

然后,如果novalue存在,则指定一个对象,并更新
NewValue
CreatedDate

const
数据=[{ItemNo:“01”,字段:“价格”,OldValue:“100”,NewValue:“200”,CreatedDate:“17/10/2020”},{ItemNo:“01”,字段:“价格”,OldValue:“200”,NewValue:“300”,CreatedDate:“18/10/2020”},{ItemNo:“01”,OldValue:“100”,NewValue:“400”,CreatedDate:“19/10/2020”},{ItemNo:“01”,字段:“名称”,OldValue:“A”,NewValue:“B”,创建日期:“19/10/2020”},
结果=对象值(数据减少((r,o)=>{
常量键=['ItemNo','Field'].map(k=>o[k]);
r[键]??={…o};
r[key].NewValue=o.NewValue;
r[key].CreatedDate=o.CreatedDate;
返回r;
}, { }));
console.log(结果);

。作为控制台包装器{max height:100%!important;top:0;}
你能分享你的代码吗,你尝试了什么。好的,我做到了。。
                            var lstToDisplayNotSellingPrice = changeLogArray.filter(item => {
                            return item.Field != 'Selling Price'
                        })

                        var lstToDisplaySellingPrice  = changeLogArray.filter(item => {
                            return item.Field == 'Selling Price'
                        })

                        var resultChangeLogSellingPrice = []


                        changeLogArray.forEach((item, index) =>{
                            
                            var distinctItemIndex
                            if(index == 0 || item.ItemNo != lstToDisplaySellingPrice[index-1].ItemNo){
                                resultChangeLogSellingPrice.push(item)
                                distinctItemIndex = index
                            }else{
                                if(item.FieldLevel == lstToDisplaySellingPrice[index-1].ItemNo){
                                    resultChangeLogSellingPrice.pop()
                                    var itemLog = lstToDisplaySellingPrice[index-1]
                                    itemLog.NewValue = item.NewValue
                                    itemLog.CreatedDate = item.CreatedDate
                                    resultChangeLogSellingPrice.push(itemLog)
                                }
                            }

                        });