下面的代码运行一个函数,以从两个数组中删除撇号,然后将它们进行比较以查看它们是否相同。
var array1 = ["ohara"];
var array2 = ["o'hara"];
function convertSpecial(a,b,c) {
let aCopy = [...a];
for (let i = 0; i < aCopy.length; i++) {
if (aCopy[i].includes(b)) {
if (c == '') {
aCopy[i] = aCopy[i].replace(b,c);
} else {
aCopy[i] = aCopy[i].replace(b,c).split(' ');
aCopy = aCopy.flat();
}
}
}
return aCopy;
}
var changed1 = convertSpecial(array1,"'","");
var changed2 = convertSpecial(array2,"'","");
console.log(changed1); // returns ["ohara"]
console.log(changed2); // returns ["ohara"]
console.log((changed1 == changed2)); //returns false, should be true
我期望两个数组相同,但事实并非如此。为什么?