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.
diff([1, 2, 3, 5], [1, 2, 3, 4, 5])
An array should be returned.
["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
should be returned
["pink wool"]
.
["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]
should be returned
["diorite", "pink wool"]
.
["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"]
should be returned
[]
.
[1, 2, 3, 5], [1, 2, 3, 4, 5]
should be returned
[4]
.
[1, "calf", 3, "piglet"], [1, "calf", 3, 4]
should be returned
["piglet", 4]
.
[], ["snuffleupagus", "cookie monster", "elmo"]
should be returned
["snuffleupagus", "cookie monster", "elmo"]
.
[1, "calf", 3, "piglet"], [7, "filly"]
should be returned
[1, "calf", 3, "piglet", 7, "filly"]
. the hints given for Arr.slice (NUMBER1,NUMBER2) Extract the number between number1 to number2 in arr to form a new array and return without changing the original array;arr. Filter (Callbackfunction () {}) returns an array of values that are tested by Callbackfunction in Arr, returned as an array;Arr.indexof (item) finds item in ARR, returns the index value corresponding to item, does not find return-1;arr. concat (ARR2 or numbers) merges arr2 or numbers with the original array arr to return the new array.
The previous query is heavy, so consider using the previous function here.
function differ (ARR1,ARR2) {
var newarr=arr1.concat (ARR2); The arr1 and arr2 are synthesized into a new array to be used, and the edges are queried in newarr with arr1 and arr2;
var temp=newarr.filter (function (Item,index,array) {
return Arr1.indexof (item) ==-1 | | Arr2.indexof (item) ==-1; Newarr elements that are not found in arr1 or arr2 are needed.
});
return temp;
}
Freecodecamp to filter out two different parts of an array