Diff two arrays chaining filter

I want to know if I can chain two filters together? I’m not sure why this doesn’t work

function diffArray(arr1, arr2) {
  var newArr = [];
  var newArr2=[];
  // Same, same; but different.
  
  newArr=arr1.filter(el1=>!arr2.indexof(el1)>-1).filter(el2=>!arr1.indexof(el2)>-1);

  console.log(newArr);
  console.log(newArr2);
  return newArr.concat(newArr2);
}

console.log(diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]));

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/diff-two-arrays

Yes, you can but you have some issues with your current code.

  1. indexof is not a valid array method. JavaScript is case-sensitive.

  2. In your return statement return newArr.concat(newArr2), there is no need to concatenate an empty array (newArr2). newArr contains the filtered part.

  3. Once you resolve #1 above, do you really want to filter the first filtered array with the second filter? To know for sure, try assigning the result of the first filter to a variable and console.log it and then think about if that result being filtered the way your second filter works is correct.

Thanks, I’ll return to this tomorrow. It’s like 1:30 am here.

I see what you mean; I am filtering an empty array. Thanks for the help.

What I was wanting you to understand is that the first array created from the first filter method creates a specific array (not empty). The second filter which filters that array will always return an empty array, because of the logic in the callback function.