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

Array.filter()

Array.filter() ECMAScript 5


return array elements that pass a predicate

Synopsis

array.map(predicate) array.map(predicate, o)

Core JavaScript Reference | 725

Array.forEach()

Arguments

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.

Returns

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

Description

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.

Example

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

See Also

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

Array.forEach() ECMAScript 5

invoke a function for each array element

Synopsis

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

Arguments

f The function to invoke for each element of array.

o An optional value on which f is invoked.

Array.indexOf()

Returns

This method returns nothing.

Description

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.

Example

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.map() , Array.reduce()

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/28131.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1