We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
原文: https://gist.github.com/ljharb/58faf1cfcb4e6808f74aae4ef7944cff
forEach
callback
item
index
list
undefined
['zhang', 'zhen', 'qiang'].forEach((val, index, thisArg) => { console.log(index, val); console.log(thisArg); });
map:
map
const objects = [{ id: 'a', name: 'chengyaojin' }, { id: 'b', name: 'zhangjunhui' }, { id: 'c', name: 'zhangzhenqiang' }]; const res = objects.map((item, index, list) => { item['ctime'] = Date.now(); return item; }); console.log(res);
filter:
filter
const ints = [1, 2]; const events = ints.filter(item => { return item % 2 === 0; }); console.log(ints === events, events);
reduce:
reduce
result
// 注意:`reduce`和`reduceRight`在`callback`参数后都一个可选的`initalValue`参数, // 如果忽略不填,则默认是第一项的值 const sum = [1, 2, 3].reduce((result, item) => { return result += item; }, 0); console.log(sum);
reduceRight: 和reduce相同,只不过遍历的方向是相反的方向
reduceRight
some:
some
predicate
answers
true
false
注意
const hasNegativeNumbers = [1, 2, 3, -1, 4].some((item, index) => { return item < 0; }); console.log(hasNegativeNumbers);
every:
every
const allPositiveNumbers = [1, 2, 3, -1, 4].every((item, index) => { return item > 0; }); console.log(allPositiveNumbers);
find:
find
const objects = [{ id: 'a', name: 'chengyaojin' }, { id: 'b', name: 'zhangjunhui' }, { id: 'c', name: 'zhangzhenqiang' }]; const res = objects.find((item, index, list) => { return item.id === 'c'; }); console.log(res);
findIndex:
findIndex
-1
const objects = [{ id: 'a', name: 'chengyaojin' }, { id: 'b', name: 'zhangjunhui' }, { id: 'c', name: 'zhangzhenqiang' }]; const res = objects.findIndex((item, index, list) => { return item.id === 'c'; }); console.log(res);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
JavaScript 数组操作
forEach
callback
回答: 给你一个值, 随便你对它做什么, 我不管callback
参数:item
,index
,list
undefined
map
:item
,index
,list
filter
:item
,index
,list
reduce
:callback
回答(answers): 这是前一次迭代的返回结果,我应该继续下去吗?callback
的参数:result
,item
,index
,list
reduceRight
: 和reduce
相同,只不过遍历的方向是相反的方向some
:callback
是个谓词(predicate
) - 应该返回真值或假值callback
回答(answers
): 这个值满足需求吗?callback
的参数:item
,index
,list
true
,否则返回false
注意
: 如果callback
返回true
则会停止迭代every
:callback
是个谓词 - 返回真值(truthy)或假值(falsy)callback
回答(answers
): 这个值满足需求吗?callback
的参数:item
,index
,list
true
,否则返回false
注意
: 如果callback
返回false
则会停止迭代find
:callback
是个谓词 - 返回真值(truthy)或假值(falsy)callback
回答:这是你要找的值吗?callback
的参数:item
,index
,list
undefined
callback
函数返回真值,迭代就会停止findIndex
:callback
是个谓词 - 返回真值(truthy)或假值(falsy)callback
回答:这是你要找的值吗?callback
的参数:item
,index
,list
-1
callback
函数返回真值,迭代就会停止The text was updated successfully, but these errors were encountered: