I have two arrays
const originalArray= ["form1","form2","form3","form4","form5"];
const modifiedArray = ["form2","additionForm1","form1","form3","additionForm2"];
expected results is,
let testScript = [form1 is moved to position 2,
form2 is moved to position 0,
form3 is moved to position 3,
from 4 is deleted position is 3]
form5 is deleted position is 4,
additional1 added to position 1
additional2 added to position 4
]
I can get deleted data using follwing code, But when try to get moved data and position changed data I faced some problem.
this is what I tried,
function testFunc(){
const originalArray = ["form1","form2","form3","form4","form5"];
const modifiedArray= ["form2","additionForm1","form1","form3","additionForm2"];
let flag = false;
let result = [];
originalArray.forEach((Nelement, Nindex) => {
modifiedArray.forEach((Telement, Tindex) => {
if(Nelement === Telement){ //check originalArray data containg the modified array
flag = true;
//return false;
}
if(flag === true && Nindex !== Tindex){ //try to get moved data
result.push(Nelement+" change the position to "+ Tindex )
}
})
if(flag !== true){ //if original array data doesn't include in modified array
result.push(Nelement+" removed from "+ Nindex + " position" )
}
flag = false
})
}