一:filter() 函数, 过滤
// 基本用法
const nums = [10,20,111,222,444,40,50]
let newNums2 = nums.filter(item => item < 100)
// 去除数组中的空元素
record = record.filter(function(n){return n})
// 遍历数组,includes是es6中的新方法,在search方法中直接返回新数组
search(keyword){
return this.urls.filter(item =>{
if(item.name.includes(keyword)){
return item
}
})
}
二:map() 函数,遍历
// 基础用法
const nums = [10, 20, 111, 222, 444, 40, 50]
let newNums2 = nums.map(item => item *2)
三:reduce() 函数,汇总
const nums = [10, 20, 111, 222, 444, 40, 50]
// reduce 高阶函数
// preVal 遍历的前一个值
// item 遍历的值
// 0 初始化的值,第一次遍历的preVal值就是这个初始化值
let total2 = nums.reduce(function (preVal,item) {
return preVal + item;
},0)
console.log("reduce:", total2);
// reduce 箭头函数
let total2 = nums.reduce(( preVal , item ) => preVal + item)
四:foreach循环(不能使用return来停止循环)
search(keyword){
var urls = [....] // 处理的元素的集合
var newList = []
urls.forEach(item =>{
if(item.name.indexOf(keyword) != -1){
newList.push(item)
}
})
return newList
}
五:findIndex
返回true后index就是到匹配的元素索引
var urls = [....] // 处理的元素的集合
var index = urls.findIndex(item =>{
if(item.name == row.name){
return true;
}
})
urls.splice(index, 1)
六:some
如果匹配成功就return true跳出some的循环
var urls = [....] // 处理的元素的集合
urls.some((item, i) =>{
if(item.name == 'str'){
this.urls.splice(i, 1)
return true;
}
})