Diff two Arrays (compare two arrays)
1. Requirements
- Compares two arrays and returns a new array
- The elements of the array are two of all unique array elements in a given array. In other words, returns the difference of two arrays.
2. Ideas
- Defines a new array variable that joins the two arrays entered with. Concat () to assign to it
- Defines a check function that returns all the unique array elements in a given array of two
- Filter out the eligible elements of a new array variable with. Filter ()
3. Code
function diff(arr1, arr2) {var newArr = [];newArr = arr1.concat(arr2);function check(item){ if(arr1.indexOf(item) === -1 ||arr2.indexOf(item) === -1){ return item; }}return newArr.filter(check);}diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
4. RELATED LINKS
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
5. Blog notes: Future blogs are written in Mardown syntax
Diff. Arrays-freecodecamp algorithm Topic