jquery uses the $.grep () method

Source: Internet
Author: User

I have not used the $.grep () method before, and the $.grep () method filters the array according to a certain condition, so using the $.grep () method can filter out the results we want from the array. Let's look at an example with an array named Nums:

var nums = ' 1,2,3,4,5,jquery,css,5 '. Split (', ');

There are numbers and strings in the visible array, and if we want to find the strings in them, we can use the $.grep () method directly to accomplish this task, as follows:

function (num, index) {    //  num = current value  of the array element    //  index = subscript of    the current value return  // The result is: ["JQuery", "CSS"  ]

It's easy to think of the $.map () method, the $.map () method can convert an array to another array, so using the $.map () method can also accomplish this task, as follows:

function (num, index) {    // and $.grep () difference    //return IsNaN, get result: [True, True]     returnnull//

This article briefly introduces the use of the lower $.grep () method, and compares the next $.map () method. As you can see, the proper use of JQuery's built-in approach simplifies our programs.

jquery uses the $.grep () method

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.