Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
¿Cómo obtener el índice y el valor comparando dos matrices que cambiaron la posición y eliminaron?

tengo dos matrices

 const originalArray= ["form1","form2","form3","form4","form5"]; const modifiedArray = ["form2","additionForm1","form1","form3","additionForm2"];

resultados esperados es,

 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 ]

Puedo obtener datos eliminados usando el siguiente código, pero cuando intento mover datos y cambiar la posición de los datos, me encontré con un problema.

esto es lo que probé,

 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 Respostas
Responde à pergunta

0

Esta es una pregunta interesante. Esto debería funcionar y el resultado es

"form1 se mueve a la posición 2",
"form2 se mueve a la posición 0",
"form3 se mueve a la posición 3",
"form4 se elimina la posición es 3",
"form5 se elimina la posición es 4",
"additionForm1 agregado a la posición 1",
"additionForm2 añadido a la posición 4"

Uso otro ciclo para mostrar los elementos agregados a la matriz.

 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 Relatório

0

simplemente reemplace el siguiente fragmento

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

a

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

Esto se debe a que flag no se desarma en el segundo bucle.

solución completa a continuación.

 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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda