Freecodecamp to filter out two different parts of an array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.