0

The below code runs a function to remove apostrophes from two arrays then compares them to see if they are the same.

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

I am expecting the two arrays to be the same, but they are not. Why?

Sweepster
  • 1,829
  • 4
  • 27
  • 66
  • Try `console.log([1]===[1])` and see what happens... Also, to fix your problem, have a look here: [How to compare arrays in JavaScript?](https://stackoverflow.com/q/7837456/5768908). – Gerardo Furtado Apr 03 '19 at 23:40
  • Two different arrays will never be equal even if they have the same elements. – James Apr 03 '19 at 23:42

0 Answers0