[英]How to compare two arrays in javascript and remove the similar values?
I'm new to javascript and web development.我是 javascript 和 web 开发的新手。
I am planning to compare two Strings and remove the same elements from the said strings.我打算比较两个字符串并从所述字符串中删除相同的元素。
for example:例如:
str1 = "john" str2 = "jane" str1 = “约翰” str2 = “简”
str1 and str2 both have "j" and "n", and therefore removed from the string it's supposed to look like this: str1Res = "oh" str2Res = "ae" str1 和 str2 都有“j”和“n”,因此从字符串中删除它应该是这样的:str1Res = "oh" str2Res = "ae"
this is my code.这是我的代码。 the problem is: the firstName variable is correctly processed.
问题是:正确处理了 firstName 变量。 However, the secondName doesn't remove anything.
但是,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);
}
}
}
output: firstName: 'o','h' secondName: 'j','a','n', 'e' thanks in advance! output:名字:'o','h' secondName:'j','a','n','e'提前致谢!
You can use regex to do that:您可以使用正则表达式来做到这一点:
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,""))
This is probably too advanced, but you can do this using regular expressions, like this:这可能太高级了,但是您可以使用正则表达式来做到这一点,如下所示:
var name1 = ...;
var name2 = ...;
var rx = new RegExp('[' + name2 + ']',"g");
name1 = name1.replace(rx,"");
Within regex, the brackets ('[' and ']') turn the string into a collection of characters to match在正则表达式中,方括号('[' 和 ']')将字符串转换为要匹配的字符集合
The problem in your code was you making the duplicated arrays that incorrectly, you need to copy arrays like that:您的代码中的问题是您错误地复制了 arrays,您需要像这样复制 arrays:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.