JS Array filter Pop push shift Unshift method

Source: Internet
Author: User
Tags square root javascript array javascript array map

JavaScript Array Filter () method

JavaScript Array Object

Instance

Returns an element that has all elements greater than 18 in the array ages :

var ages = [ 40];

function Checkadult (age) {
return age >= 18;
}

function MyFunction () {
document.getElementById ("Demo"). InnerHTML = Ages.filter (Checkadult);
}

The output is:

32,33,40
Try it» Definition and usage

The filter () method creates a new array of elements in the new array by examining all the elements in the specified array that meet the criteria.

Note: filter () does not detect empty arrays.

Note: filter () does not change the original array.

Browser support

The number in the table represents the version number of the first browser that supports the method.

Method          
Filter () Yes 9 1.5 Yes Yes
Grammar
Array.  Filter(function(currentvalue,index,arr), thisvalue)  
Parameter description
Parameters Description
function (CurrentValue, Index,arr) Have to. function, each element in the array executes the function
Function parameters:
Parameters Description
CurrentValue Have to. The value of the current element
Index Optional. Index value of the current element
Arr Optional. The array object to which the current element belongs
Thisvalue Optional. The object is used as the execution callback, passed to the function, and used as the value of "this".
If Thisvalue is omitted, the value of "This" is "undefined"
Technical details

return value: Returns an array that contains all the elements that meet the criteria. An empty array is returned if there are no elements that meet the criteria.
JavaScript version: 1.6

JavaScript Array Map () method

JavaScript Array Object

Instance

Returns an array in which the elements are the square root of the original array:

var numbers = [4, 9, 25];

function MyFunction () {
x = document.getElementById ("demo")
x.innerhtml = Numbers.map (MATH.SQRT);
}

The output is:

2,3,4,5
Try it» Definition and usage

The map () method returns a new array in which the elements of the array call the function-processed values for the original array element.

The map () method processes the elements sequentially, in the order of the original array elements.

Note: map () does not detect empty arrays.

Note: map () does not change the original array.

Browser support

The number in the table represents the version number of the first browser that supports the method.

Method          
Map () Yes 9 1.5 Yes Yes
Grammar
Array.  Map(function(currentvalue,index,arr), thisvalue)  
Parameter description
Parameters Description
function (CurrentValue, Index,arr) Have to. function, each element in the array executes the function
Function parameters:
Parameters Description
CurrentValue Have to. The value of the current element
Index Optional. Index value of the current element
Arr Optional. The array object to which the current element belongs
Thisvalue Optional. The object is used as the execution callback, passed to the function, and used as the value of "this".
If Thisvalue is omitted, the value of "This" is "undefined"
Technical details

return value: Returns a new array in which the elements of the array call the function-processed values for the original array element.
JavaScript version: 1.6
JavaScript Array Some () method

JavaScript Array Object

Instance

Detects if there are elements greater than 18 in the array:

var ages = [3, Ten, 20];

function Checkadult (age) {
return age >= 18;
}

function MyFunction () {
document.getElementById ("Demo"). InnerHTML = Ages.some (Checkadult);
}

The output is:

True
Try it» Definition and usage

The Some () method is used to detect whether an element in an array satisfies a specified condition (provided by a function).

The Some () method executes each element of the array sequentially:

    • If an element satisfies a condition, the expression returns True , and the remaining elements no longer perform instrumentation.
    • Returns False if no element satisfies the condition.

Note: some () does not detect empty arrays.

Note: some () does not change the original array.

Browser support

The number in the table represents the version number of the first browser that supports the method.

Method          
Some () Yes 9 1.5 Yes Yes
Grammar
Array.  Some(function(currentvalue,index,arr),thisvalue)  
Parameter description
Parameters Description
function (CurrentValue, Index,arr) Have to. function, each element in the array executes the function
Function parameters:
Parameters Description
CurrentValue Have to. The value of the current element
Index Optional. Index value of the current element
Arr Optional. The array object to which the current element belongs
Thisvalue Optional. The object is used as the execution callback, passed to the function, and used as the value of "this".
If Thisvalue is omitted, the value of "This" is "undefined"
Technical details

return value: Boolean value. False if there are elements in the array that satisfy the condition to return true.
JavaScript version: 1.6

In JavaScript scripts, the access rules for the stack data structure are LIFO (LIFO), and the access rules for the queue data structures are FIFO (first-in-first-out, first-in-one-out).
The queue adds items at the end of the list, removing items from the front end of the list.
Because push () is a way to add items to the end of an array, you need only one method to get items from the front of the array at a time to simulate the queue.
The array method that implements this is shift (), which moves the first item in the array and returns the item, minus 1 of the length.
Using the shift () and push () methods together, you can use arrays as you would with queues:

Copy Codecode example: var colors = new Array (); Create an array
var count = Colors.push ("Red", "green"); Push in two items
alert (count); 2
Count = Colors.push ("Black"); Push into another item
alert (count); 3
var item = Colors.shift (); Get the first item
alert (item); "Red"
alert (colors.length); 2

ECMAScript also provides an array of unshift () methods for the arrays. Unshift () Shift () uses the opposite: it can add any item to the front of the array and return the length of the new array.
Therefore, using the Unshift () and Pop () methods simultaneously, you can simulate the queue in the opposite direction by adding items to the front end of the array, removing items from the end of the array, for example:

Copy Codecode example:

var colors = new Array (); Create an array
var count = Colors.unshift ("Red", "green"); Push in two items
alert (count); 2

Count = Colors.unshift ("Black"); Push into another item
alert (count);

var item = Colors.pop (); Get the last one
alert (item); "Green"
alert (colors.length); 2

IE has a deviation from the implementation of JavaScript, and its unshift () method always returns undefined instead of the new length of the array.

JS Array filter Pop push shift Unshift 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.