Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

465
Views
¿Cómo comparar dos matrices en javascript y eliminar los valores similares?

Soy nuevo en javascript y desarrollo web.

Estoy planeando comparar dos cadenas y eliminar los mismos elementos de dichas cadenas.

por ejemplo:

str1 = "juan" str2 = "jane"

str1 y str2 tienen "j" y "n" y, por lo tanto, eliminados de la cadena, se supone que deben verse así: str1Res = "oh" str2Res = "ae"

este es mi codigo el problema es: la variable firstName se procesa correctamente. Sin embargo, el segundo nombre no elimina nada.

 var firstName = "john" var secondName = "jane" firstName = firstName.toLowerCase(); secondName = secondName.toLowerCase(); //remove whitespaces and split to array var firstNameArray = firstName.replaceAll(" ", "").split(''); var secondNameArray = secondName.replaceAll(" ", "").split(''); var firstNameRes = []; var secondNameRes = []; //duplicate array firstNameRes = firstNameArray; secondNameRes = secondNameArray; let i, j //loop the first name for (i = 0; i < firstNameArray.length; i++) { for (j = 0; j < secondNameArray.length; j++) { //pair characters of first name with characters from second name if (firstNameArray[i] == secondNameArray[j]) { //remove the element from the result array firstNameRes.splice(i, 1); } } } //loop the second name for (i = 0; i < secondNameArray.length; i++) { //pair characters of second name with characters from first name for (j = 0; j <firstNameArray.length; j++) { //remove the element from the result array if (secondNameArray[i] == firstNameArray[j]){ secondNameRes.splice(i, 1); } } }

salida: firstName: 'o', 'h' secondName: 'j', 'a', 'n', 'e' ¡gracias de antemano!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puedes usar expresiones regulares para hacer eso:

 var firstName = "john" var secondName = "jane" var reg1 = new RegExp("["+firstName+"]","g") var reg2 = new RegExp("["+secondName+"]","g") console.log(firstName.replace(reg2,"")) console.log(secondName.replace(reg1,""))

about 4 years ago · Juan Pablo Isaza Report

0

Probablemente esto sea demasiado avanzado, pero puede hacerlo usando expresiones regulares, como esta:

 var name1 = ...; var name2 = ...; var rx = new RegExp('[' + name2 + ']',"g"); name1 = name1.replace(rx,"");

Dentro de expresiones regulares, los corchetes ('[' y ']') convierten la cadena en una colección de caracteres para que coincida

about 4 years ago · Juan Pablo Isaza Report

0

El problema en su código fue que hizo las matrices duplicadas incorrectamente, necesita copiar matrices así:

 var firstName = "john" var secondName = "jane" firstName = firstName.toLowerCase(); secondName = secondName.toLowerCase(); //remove whitespaces and split to array var firstNameArray = firstName.replaceAll(" ", "").split(''); var secondNameArray = secondName.replaceAll(" ", "").split(''); var firstNameRes = [...firstNameArray]; // Here the way we copy the ARRAY elements, it doesn't work like strings. var secondNameRes = [...secondNameArray]; // I CHANGED ALL CODE BELOW FOR YOU TO WORK let indexToRemoveFirstName = []; let indexToRemoveSecondName = []; //loop to get indexes for (let i = 0; i < firstNameArray.length; i++) { for (let j = 0; j < secondNameArray.length; j++) { //pair characters of first name with characters from second name if (firstNameArray[i] == secondNameArray[j]) { //storing the indexes to remove from originals arrays indexToRemoveFirstName.push(i); indexToRemoveSecondName.push(j); } } } //loops to remove from Array after having the indexes, it needs to loop backwards: for (let i = indexToRemoveFirstName.length-1; i >= 0; i--){ firstNameArray.splice(indexToRemoveFirstName[i], 1) } for (let y = indexToRemoveSecondName.length-1; y >= 0;y--){ secondNameArray.splice(indexToRemoveSecondName[y], 1) } console.log(firstNameArray); console.log(secondNameArray);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!