JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:


Array.filter() ECMAScript 5

return array elements that pass a predicate

Synopsis, o)

Core JavaScript Reference | 725



predicate The function to invoke to determine whether an element of array will be included in the returned array.

o An optional value on which predicate is invoked.


A new array containing only those elements of array for which predicate returned true (or a truthy value).


filter() creates a new array and then populates it with the elements of array for which the predicate function returns true (or a truthy value). The filter() method does not modify array itself (though the predicate function may do so).

filter()loops through the indexes of array, in ascending order, and invokes predicate once for each element. For an index i, predicate is invoked with three arguments, predicate(array[i], i, array)

If predicate returns true or a truthy value, then the element at index i of array is appended to the newly created array. Once filter() has tested each element of array it returns the new array.

See Array.forEach() for further details.


[1,2,3].filter(function(x) { return x > 1; }); // => [2,3]

See Also

Array.every() , Array.forEach() , Array.indexOf() , , Array.reduce()

Array.forEach() ECMAScript 5

invoke a function for each array element


array.forEach(f) array.forEach(f, o)


f The function to invoke for each element of array.

o An optional value on which f is invoked.



This method returns nothing.


forEach()loops through the indexes of array, in ascending order, and invokes fonce for each element. For an index i, f is invoked with three arguments:

f(array[i], i, array)

The return value, if any, of f is ignored. Note that forEach() does not have a return value. In particular, it does not return array.

Array Method Details

The following details apply to the forEach() method, and also to the related methods map(), filter(), every(), and some().

Each of these methods expects a function as its first argument and accepts an optional second argument. If a second argument ois specified, then the function is invoked as if it was a method of o. That is, within the body of the function, this will evaluate to o. If the second argument is not specified, then the function is invoked as a function (not a method) and this will be the global object in non-strict code or null in strict code.

Each of these methods notes the length of arraybefore it begins looping. If the invoked function appends new elements to array, those newly-added elements will not be looped over. If the function alters existing elements that have not yet been looped over, it is the altered values that will be passed.

When invoked on sparse arrays, these methods do not invoke the function for indexes at which no element actually exists.


var a = [1,2,3];

a.forEach(function(x,i,a) { a[i]++; }); // a is now [2,3,4]

See Also

Array.every() , Array.filter() , Array.indexOf() , , Array.reduce()

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1