var first = [...arr1];
var second = [...arr2];
var result = first.filter(x => second.indexOf(x) === -1);
result = result.concat(second.filter(x => first.indexOf(x) === -1));
The most important thing is that it works, congrats!
- Never use
var again to declare variables. Get used to using
- The first two lines are creating copies of the arrays passed into the function. You aren’t modifying the arrays though, you are just creating a new array that is a result of filtering the two arrays passed in. So you don’t need the first two lines at all, they are basically using up extra unneeded memory. Just refer to the original arrays passed into the function when filtering to create the new array.
- You can combine the last two lines into one line that returns the result of the concat.
Doing all of the above You can reduce your function body down to two nice concise lines.
Okay, thank you for some great feedback! Your suggestions make a lot of sense, I’ll keep them in mind as I go!