Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
How to get index and value comparing two arrays which was changed the position and deleted?

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
    })
  }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This is an interesting question. This should work and the result is

"form1 is moved to position 2",
"form2 is moved to position 0",
"form3 is moved to position 3",
"form4 is deleted position is 3",
"form5 is deleted position is 4",
"additionForm1 added to position 1",
"additionForm2 added to position 4"

I use another loop to display the added items to the array

          const originalArray = ["form1","form2","form3","form4","form5"];
  const modifiedArray= ["form2","additionForm1","form1","form3","additionForm2"];
  let flag = false;
  let result = []
  
function testFunc(){
    //Find added items between the two lists
  originalArray.forEach((Nelement, Nindex) => { 
     
      modifiedArray.forEach((Telement, Tindex) => {
 
            if(Nelement === Telement){ //check originalArray data containg the modified array
              flag = true;
           
            if(Nindex !== Tindex)//try to get moved data
              result. push(Nelement+" is moved to position "+ Tindex )
            }
  })
     
      if(!flag){ //if original array data doesn't include in modified array
          result.push(Nelement+" is deleted position is "+ Nindex)
      }
     
        flag = false    
    })
//check added item
let arr = modifiedArray.filter(x => !originalArray.includes(x));
for(let j in arr)
result.push(arr[j]+" added to position "+modifiedArray.indexOf(arr[j]))
console.log(result)
  }
  testFunc()


   

about 4 years ago · Juan Pablo Isaza Denunciar

0

just replace following snippet

if(flag === true && Nindex !== Tindex){ //try to get moved data
              result.push(Nelement+" change the position to "+ Tindex )
            }

to

if(Nelement === Telement && Nindex !== Tindex){ //try to get moved data
      result.push(Nelement+" change the position to "+ Tindex )
 }

This is because flag is not getting unset in the second loop.

complete solution below.

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(Nelement === Telement && 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;
    })
return result;
  }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda