Ilter methods in JavaScript

Source: Internet
Author: User
Tags call back

array1.filter(callbackfn[, thisArg])
Parameters

Parameters

Definition

Array1

necessary. Array object.

Callbackfn

necessary. a function that accepts up to three arguments. The filter method calls the CALLBACKFN function once for each element in the array.

Thisarg

Select the option. This key can be used to refer to an object in a CALLBACKFN function. if Thisarg is omitted, the value of this is undefined.

return value

A new array that contains all the elements of the callback function pin that return true to the source array. if the callback function needle returns false to all elements of the array1 , the length of the new array is 0.

Exceptions

If the CALLBACKFN argument is not a function object, it sidearm back to the TypeError exception.

Registration

The filter method calls the CALLBACKFN function once for each element in the array (in order to increment the index). the callback function skips the element that is missing from the array.

In addition to array objects, the filter method can be used for any object that is of length and has a value index of the name.

Call back Function language method

The language of the callback function is as follows:

function Callbackfn (value, index, array1)

You can use up to three parameters to declare a callback function.

The following table lists the callback function parameters.

Call Back Arguments

Definition

Value

The value of the array element.

Index

The index of the number of array elements.

Array1

The array object that contains the element.

Modifying array objects

The filter method does not directly modify the original array, but the callback function can be modified. The following table shows the results of modifying array objects after the Activate filter method.

The state of the filter method after the activation.

Will the elements be transferred to the callback function?

Add elements where the original length of the array is exceeded.

Whether.

The added element is filled to the location where the element in the array is missing.

Yes (if the index has not been transferred to the callback function).

Elements have changed.

Yes (if the element has not been transferred to the callback function).

Removes the element from the array.

No (unless the element has been transferred to a callback function).


In the following example, thecallbackfn argument contains the code for the callback function.

Create the original array. var arr = [5, "element", ten, "the", true];  Create An array that contains the string//values is in the original array. var result = Arr.filter (     function (value) {         return (typeof value = = = ' string ');     });  document.write (result); Output:element, the

  

Ilter methods in JavaScript

Related Article

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.