Diff. Arrays
Compares two arrays and returns a new array whose elements are two of all unique array elements in a given array. In other words, returns the difference of two arrays.
Comparisonoperators
Array.slice ()
Array.filter ()
Array.indexof ()
Array.concat ()
Ideas:
First find the arr1 in the different parts of the arr2 to form a new array 1, and then find the arr2 in the different parts of the arr1 form a new array 2, the two arrays are merged to produce two different parts of the array.
Knowledge Points:
(1) var newarr=arr.filter (func);
The filter function can filter out elements that allow the Func function to return true and form a new array, and the Func function defaults to a single element in the value array;
1 var newArr1 = arr1.filter (COMP1); 2 var
(2) Arr.indexof (value) ===-1;
The IndexOf function can be found on an array, and the number returned is the index of the value in arr if it can find value in arr.
1 function Comp1 (value) {2 if (Arr2.indexof (value) = = =-1) {3 return true ; 4 }5 }
(3) NewArr = Newarr1.concat (NEWARR2);
The CONCAT function can implement a combination of two arrays, newArr1 and newArr2 to form a new array.
1 newArr = Newarr1.concat (NEWARR2);
Code:
1 functiondiff (Arr1, arr2) {2 varNEWARR = [];3 4 functionComp1 (value) {5 if(Arr2.indexof (value) = = = 1) {6 return true;7 }8 }9 Ten functionCOMP2 (value) { One if(Arr1.indexof (value) = = = 1) { A return true; - } - } the varNEWARR1 =Arr1.filter (COMP1); - varNEWARR2 =Arr2.filter (COMP2); -NEWARR =Newarr1.concat (NEWARR2); - returnNEWARR; + } - diff ( +["Andesite", "Grass", "Dirt", "pink wool", "dead Shrub"], ["Diorite", "andesite", "Grass", "Dirt", "dead Shrub"]);
FCC Intermediate algorithm problem comparison two arrays