A summary of JS filter () Usage

Source: Internet
Author: User
/* Filter () each element in the array executes a specified function (callback), and a new array is created, which is the original array element whose return value is true when all callback functions are executed.

It executes the specified function only for non-null elements in the array, the elements that are not assigned or have been deleted are ignored, and the newly created array does not contain those elements.
The callback function can have three parameters: the current element, the index of the current element, and the current array object.  *///raw data var arrs = [{name: "Tom", Age:18, Sex: "Boy"}, {name: "Jim", Age:19, Sex:
  "Boy"}, {name: "anchor", Age:20, Sex: "Boy"}, {name: "Lucy", Age:18, Sex: "Girl"

}, {name: "Lily", age:19, Sex: "Girl"}, {name: "Andy", Age:20, Sex: "Girl"}];

Filter condition var limits = {name: ' Tom ', age:18, Sex: ' Boy '}; 
    Filter callback functions function Dofilter (element, index, array) {if (limits.name && limits.name!= element.name) {//Name filter
  return false;
  }else if (limits.age && limits.age!= element.age) {//Age filter return false;
  }else if (limits.sex && limits.sex!= element.sex) {//sex filter return false;
return true;
/* Test */var filtered = Arrs.filter (dofilter); ConsoLe.log (filtered); 

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.