Let's imagine we have 2 sets of data arrays, A (original) and B (updated).
var A = [
{ id: 1, value: 'Product Name 1' },
{ id: 2, value: 'Product Name 2' },
{ id: 3, value: 'Product Name 3' },
{ id: 4, value: 'Product Name 4' },
{ id: 5, value: 'Product Name 5' }
]
var B = [
{ id: 1, value: 'Product Name 1' },
{ id: 2, value: 'Changed Name' },
{ value: 'New Product' }
]
The goal is to compare both arrays, identifying differences such as deleted items from array A that are not present in array B, edited items with changed 'value' property, and new items added without an id.
A logic for comparison can be summarized as follows (assuming each A and B are one element from their respective arrays)
If A.id == B.id and A.value !== B.value then it's an Edit
If B.id doesn't exist in A then it's a New item
If B.id is not found in A then it's Deleted
We need to create arrays for Added, Edited, and Deleted elements.
The expected result arrays would look like this:
Added = [
{ value: 'New Product'}
]
Edited = [
{ id: 2, value: 'Changed Name' }
]
Deleted = [
{ id: 3, value: 'Product Name 3' },
{ id: 4, value: 'Product Name 4' },
{ id: 5, value: 'Product Name 5' }
]