详细:https://blog.csdn.net/weixin_44999830/category_8974432.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var arr = ['yellow', 'orange', 'bloack', 'pink', 'red'];
// 1. forEach迭代 遍历
arr.forEach(function(value) {
if (value == 'orange') {
console.log('找到了orange元素');
// 在forEach 里面 return 不会终止迭代
return true;
}
console.log(11);
})
// 如果查询数组中唯一的元素, 用some方法更合适,
arr.some(function(value) {
if (value == 'red') {
console.log('找到了red元素');
// 在some 里面 遇到 return true 就是终止遍历 迭代效率更高
return true;
}
console.log(22);
});
arr.filter(function(value) {
if (value == 'pink') {
console.log('找到pink元素');
// filter 里面 return 不会终止迭代
return true;
}
console.log(33);
});
</script>
</body>
</html>