JS:对象的聚合数组

JS: Aggregate Array of object

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

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

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

我想将 Field Price 的唯一变化合并到一行中 我想要的结果: 只有一行用于更改 Field=Price、第一条记录的 OldValue、最后一条记录的 NewValue (orderby createdDate)

[{"ItemNo":"01", "Field":"Price","OldValue":"100","NewValue":"400","CreatedDate":"19/10/2020"},
{"ItemNo":"01", "Field":"Name","OldValue":"A","NewValue":"B","CreatedDate":"19/10/2020"}]

这就是我现在拥有的;

                            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)
                                }
                            }

                        });

我尝试先分离出 Field=Selling Price 的数组,然后使用仅包含 Selling Price 变化的数组,并在每个 ItemNo 上使用 forEach 如果当前行与前一行具有相同的 ItemNo,则弹出上一个日志然后推送具有当前行的 NewValue 和 CreatedDate 的新日志。 最后,我会将价格变化列表和其他变化列表合并到结果数组中

通过 CreatedDate 排序的项目数组,您可以使用 ItemNoField.

对对象进行分组

如果没有值存在则分配一个对象并更新NewValueCreatedDate.

const
    data = [{ 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" }],
    result = Object.values(data.reduce((r, o) => {
        const key = ['ItemNo', 'Field'].map(k => o[k]);
        r[key] ??= { ...o };
        r[key].NewValue = o.NewValue;
        r[key].CreatedDate = o.CreatedDate;
        return r;
    }, { }));
   
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }