Here is what I went to do.
const original = [
{label: 'test1', formUid: 211},
{label: 'test2', formUid: 204},
{label: 'test3', formUid: 258},
{label: 'test4', formUid: 1045},
{label: 'test5', formUid: 1096},
{label: 'test5', formUid: 1096},
];
const modified = [
{label: 'test1', formUid: 211},,
{label: 'test4', formUid: 1045},
{label: 'test6', formUid: 1025},
{label: 'test5', formUid: 1096},
]
I have two arrays, one is original array otherone is modified array from the original array. So what happen here is I checked original array obejct include in modified array
Considering above two deleted object I am going to modify my copy of original array, So I make copy from orginal array delete the two object that I consirder as deleted. so output is like this for now
const CopyOforiginal = [
{label: 'test1', formUid: 211},
{label: 'test4', formUid: 1045},
{label: 'test5', formUid: 1096},
{label: 'test5', formUid: 1096},
];
this is happening in my code you can see It in snippet,
But this the problem now, you can see two same object in original array (I have set the index for example)
original = [
[4]:{label: 'test5', formUid: 1096},
[5]:{label: 'test5', formUid: 1096},
]
and one object in modified array same object as above objects
modified = [
[3]:{label: 'test5', formUid: 1096},
]
So How can I compare this ? See if we get first object from the original array and find modified array have an same object yeah there is a one it found same object from the modified array that finish
Then get second same object from the original array and check is modified array have any matching object with that , there have one but it counted before we cant get again and again same object So original array second object want to mark as deleted one
after fix all these problems final result want to be like this
const CopyOforiginal = [
{label: 'test1', formUid: 211},,
{label: 'test4', formUid: 1045},
{label: 'test5', formUid: 1096},
];
function testFunc(){
const original = [
{label: 'test1', formUid: 211},
{label: 'test2', formUid: 204},
{label: 'test3', formUid: 258},
{label: 'test4', formUid: 1045},
{label: 'test5', formUid: 1096},
];
const modified = [
{label: 'test1', formUid: 211},,
{label: 'test4', formUid: 1045},
{label: 'test6', formUid: 1025},
{label: 'test5', formUid: 1096},
{label: 'test5', formUid: 1096},
]
let originalCopy;
let flag;
let oForm , oIdx;
let srcipt = [];
originalCopy = [...original];
original.forEach((originForm, originInx) => {
let res = modified.some(item => item.formUid === originForm.formUid)
if(!res){
srcipt.push(originForm.formUid + " DELETE_FROM " + (originInx+1)); //created the script for deleted form
let res = originalCopy.findIndex(idx => idx.formUid === originForm.formUid); //get the index from copy of the original array
originalCopy.splice(res, 1); //remove the object
}
})
//document.getElementById("originalArray").innerHTML = JSON.stringify(original);
//document.getElementById("modifiedArray").innerHTML = JSON.stringify(modified);
document.getElementById("result").innerHTML = JSON.stringify(srcipt);
document.getElementById("copyArray").innerHTML = JSON.stringify(originalCopy);
}
<button onClick="testFunc()">Click</button>
originalArray : <p id="originalArray"></p>
modifiedArray : <p id="modifiedArray"></p>
result : <p id="result"></p>
CopyOf original Array result : <p id="copyArray"></p>