I am working on an array of change logs (object)
Each element in my array is one field change of an item. My Array looks like this
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"}]
I want to combine the only changes of Field Price into one row my wanted result outcome: Only one row for changes of Field=Price, OldValue of the first record, NewValue of the final record (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"}]
This is what I have right now;
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)
}
}
});
I tried to seperate the one that Field=Selling Price first, then use that array which contains only Selling Price changes and use forEach on each ItemNo if the current row has the same ItemNo as the previous, pop the previous log and then push new log which has the NewValue and the CreatedDate of the current row. And in the end I will combine list of Price Changes and list of other changes into the result array