Simple analysis of jquery array method for deleting the specified element: grep () _jquery

Source: Internet
Author: User

Problems encountered

We have a problem today, delete one of the specified elements in the array, and return the new array.

The JS array I defined is like this:

var sexlist=new array[3];
sexlist[0]= "1";
sexlist[1]= "2";
Sexlist[2]= "";

The effect you want to achieve

The effect I want to achieve is this:

Deletes the element of the index =1 and returns the new array.

The result returned is:

var sexlist=new Array ("1", "");

We know that native JavaScript has a function: the splice () method, which deletes the specified element in the array.

On the specific use of the splice () method, you can refer to the description of W3school, there is no more explanation: http://www.w3school.com.cn/jsref/jsref_splice.asp

Implementing code using the Splice ()

My implementation code:

var sexlist=new array[3];
sexlist[0]= "1";
sexlist[1]= "2";
Sexlist[2]= "";
Sexlist=sexlist.splice (1,1);

But when I found out that my array used this way, the returned array was not in line with expectations. Returns an empty string.

Later on the web search for jquery related APIs, found a function: grep ()

grep () Use method description

Jquery.grep (Array, callback, [invert])

Overview

Filter the array elements using the filter function.

This function passes at least two parameters: the array to be filtered and the filter function. The filter function must return true to preserve the element or false to delete the element.

Parameters

English name parameter Chinese description
Array The array to be filtered.
Callback This function processes each element of the array. The first argument is the current element, the second parameter, and the element index value. This function should return a Boolean value. In addition, this function can be set to a string, which is considered "Lambda-form" when set to a string (abbreviated form?). , where a represents the array element and I represents the element index value. such as "a > 0" representing "function (a) {return a > 0;}".
Invert If "invert" is false or set, the function returns the element in the array that returns true by the filter function and returns the set of elements in the filter function that returns False when "invert" is true.

Implementing code using GREP ()

Sexlist=$.grep (Sexlist,function (n,i) {return
i!=1;
});

function (n,i)

N: A single entity that represents an array,

I: An index that represents an array.

The above analysis of jquery array to delete the specified elements of the method: Grep () is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.