I have two arrays. I want compare modified array with original array and I want to change the modifid array same as the original array. position want to be same also
I expecting output is,
const modified = [
{label: 'cover1', formUid: 1045},
{label: 'cover2', formUid: 211},
{label: 'cover3', formUid: 204},
{label: 'cover4', formUid: 258},
{label: 'cover1', formUid: 1045},
]
but now I am getting like this,
const modified = [
{label: 'cover2', formUid: 211},
{label: 'cover3', formUid: 204},
{label: 'cover4', formUid: 258},
{label: 'cover1', formUid: 1045},
]
here is my try,
const original = [
{label: 'cover1', formUid: 1045},
{label: 'cover2', formUid: 211},
{label: 'cover3', formUid: 204},
{label: 'cover4', formUid: 258},
{label: 'cover1', formUid: 1045},
];
const modified = [
{label: 'cover4', formUid: 258},
{label: 'cover2', formUid: 211},
{label: 'cover3', formUid: 204},
{label: 'cover1', formUid: 1045},
{label: 'cover1', formUid: 1045},
]
function testFunc(original , modified){
let originalCopyForPosision;
originalCopyForPosision = [...original];
modified.forEach((element,idx) => {
let targetIndex = originalCopyForPosision.findIndex(e => e.formUid === element.formUid)
if(idx === targetIndex){
return
}else{
if(targetIndex > 0){
modified.splice(targetIndex, 1);
modified.splice(idx,0,element);
}
}
})
document.getElementById("originalArray").innerHTML = JSON.stringify(original);
document.getElementById("modifiedArray").innerHTML = JSON.stringify(modified);
}testFunc(original,modified)