如何比较 javascript 中的两个数组并删除相似的值?

How to compare two arrays in javascript and remove the similar values?

我是 javascript 和网络开发的新手。

我打算比较两个字符串并从所述字符串中删除相同的元素。

例如:

str1 = "约翰" str2 =“简”

str1 和 str2 都有“j”和“n”,因此从字符串中删除 它应该看起来像这样: str1Res = "哦" str2Res = "ae"

这是我的代码。问题是: firstName 变量被正确处理。但是,secondName 不会删除任何内容。

    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);
        }
      }
    }

输出: 名字:'o','h' secondName:'j'、'a'、'n'、'e' 提前致谢!

您可以使用正则表达式来做到这一点:

    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,""))

这可能太高级了,但是您可以使用正则表达式来做到这一点,如下所示:

var name1 = ...;
var name2 = ...;

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

name1 = name1.replace(rx,"");

在正则表达式中,方括号(“[”和“]”)将字符串转换为要匹配的字符集合

您的代码中的问题是您错误地制作了重复的数组,您需要像这样复制数组:

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);