jquery array method of deleting the specified element: grep ()

Source: Internet
Author: User

jquery array method of deleting the specified element: grep () The problem encountered by the jqueryjavascript element of the diamond array

Today, a problem is encountered, a specified element in the array is deleted, and a new array is returned.
The JS array I have defined is this:

var sexList=newArray[3];sexList[0]="1";sexList[1]="2";sexList[2]="";
Want to achieve the effect

The effect I want to achieve is this:
Deletes an element of index =1 and returns a new array.
The results returned are:

var sexList=newArray("1","");

We know that native JavaScript has a function: the splice () method, which removes the specified element from the array.
For the specific use of the splice () method, you can refer to the description of W3school, here is not much to explain: http://www.w3school.com.cn/jsref/jsref_splice.asp

Implementation code using the splice ()

My implementation code:

varsexlist=New Array[3];sexlist[0]="1"; sexlist[1]="2"; sexlist[2]=""; Sexlist=sexlist.splice (1,1);

But when I found my array using this way, the returned array was not as expected. An empty string is returned.

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

grep () How to use instructions

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 Array to be filtered.
Callback This function handles each element of the array. The first parameter 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 that, when set to a string, is considered "Lambda-form" (abbreviated form?). ), where a represents an array element, and I represents the index value of an element. such as "a > 0" means "function (a) {return a > 0;}".
Invert If "invert" is false or set, the function returns the element in the array that is true by the filter function, and when "invert" is true, returns the set of elements that return false in the filter function.
Implementation code that uses grep ()
sexList=$.grep(sexList,function(n,i){return i!=1;});

function (n,i)
N: Represents a single entity of an array,
I: Represents the index of the array.

jquery array method of deleting the specified element: grep ()

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.