Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2017.2.20 - filter()、map()、every()、some()和forEach() #11

Open
stephenLYZ opened this issue Feb 20, 2017 · 0 comments
Open

2017.2.20 - filter()、map()、every()、some()和forEach() #11

stephenLYZ opened this issue Feb 20, 2017 · 0 comments

Comments

@stephenLYZ
Copy link
Owner

ES5新增的这几个迭代数组的方法,每个方法都接受两个参数(callback[,args]),即第一个是回调函数,第二个是回调函数的this上下文。这几个方法都需要执行回调函数,唯一不同的是返回的结果。
其中回调函数接受三个参数: 元素的值、元素的索引、被迭代的数组.

filter(): 对数组中每一项执行回调函数,返回一个回调函数执行结果为true的数组,返回的是一个数组
some(): 对数组中每一项执行回调函数,一旦回调函数执行结果为true,就返回true,否则返回false,返回的是一个布尔值
every(): 对数组中每一项执行回调函数,只有每一项回调函数执行结果为true才返回true,否则返回false,返回的是一个布尔值
map(): 对数组中每一项执行回调函数,返回一个回调函数执行后的数组。
forEach(): 对数组中每一项执行回调函数,无返回值。

栗子:

const arrs = [1,2,3,4,5,6,7]

arrs.filter(function(item, index, array) { return item > 4 })  
// [5,6,7]

arrs.some(function(item, index, array) { return item > 6 })
// true

arrs.every(function(item, index, array) { return item > 6 })
// false

arrs.map(function(item, index, array) { return item + 1 })
// [2,3,4,5,6,7,8]

arrs.map(function(item, index, array) { do sth... }) 
@stephenLYZ stephenLYZ changed the title filter()、map()、every()、some()和forEach() 2017.2.20 - filter()、map()、every()、some()和forEach() Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant