JS Note--filter,includes

Source: Internet
Author: User

Title: Writing function Array_diff (A, b), passing in two arrays, a, a, a, the value of the array A is all removed, the duplicate is removed, and the new array is removed.

JavaScript] View plain Copy

    1. <span style="Font-family:microsoft yahei;font-size:14px;" >function Array_diff (A, b) {
    2. return A.filter (e =!b.includes (e));
    3. }</span>

Ex

Array_diff ([1,2,3,3,4],[3]) = [1, 2, 4]

1. Filter ()

The filter () method creates a new array of elements in the new array by examining all the elements in the specified array that meet the criteria.

Note: Filter () does not detect empty arrays.

Note: Filter () does not change the original array.

Filter: Select elements in the original array that match the criteria to form a new array

var subarr=arr.filter (function (Val,i,arr) {

return condition; All elements that return true are copied to the new array

})

2, includes ()

includes()method is used to determine whether an array contains a specified value, and if so, returns True or false, as appropriate.

let a = [123;a. Includes (2//true a. Includes (4//false             

JS Note--filter,includes

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.