这里写自定义目录标题
es6数组方法:1、map方法;2、find方法;3、findIndex方法;4、filter方法;5、every方法;6、some方法;7、reduce方法;8、reduceRight方法;9、foreach方法;10、keys方法等等
1、map方法
通过制定方法处理数组中的每一个元素,并返回处理后的数组。
var arr = [12,14,34,22,18];
var arr1 = arr.map((item,index,arr) => {
console.log("当前元素"+item);
console.log("当前索引"+index);
console.log("数组本身"+arr);
if (item>20) {
return item-10;
} else {
return item-5;
}
})
console.log(arr1) //[7, 9, 24, 12, 13]
2、find和findIndex方法
检索数组中的元素,find方法返回第一个符合要求的元素,findIndex方法返回第一个符合要求的元素下标。
var arr = [12, 14, 34, 22, 18];
var arr1 = arr.find((currentValue, index) => {
return currentValue > 20;
})
var arr2 = arr.findIndex((currentValue, index) => {
return currentValue > 20;
})
console.log(arr, arr1, arr2);//[12, 14, 34, 22, 18] 34 2
3、filter方法
检索数组中的元素,并以数组的形式返回所有符合要求的元素。
var arr = [12,14,34,22,18];
var arr1 = arr.filter((currentValue, index) => {
return currentValue>20;
})
console.log(arr,arr1);//[12,14,34,22,18],[34,22]
//逻辑属性的筛选
var arr = [
{ id: 1, text: 'aa', done: true },
{ id: 2, text: 'bb', done: false }
]
console.log(arr.filter(item => item.done)) // { id: 1, text: 'aa', done: true },
4、every方法
检测数组中的每一个元素是否符合条件,是则返回true,否则是false。
var arr = [12,14,34,22,18];
var arr1 = arr.every((currentValue, index) => {
return currentValue>20;
})
console.log(arr,arr1);//[12, 14, 34, 22, 18] false
5、some方法
查找数组中是否有满足条件的元素 返回的是一个布尔值 如果查找到第一个满足条件的元素就终止循环
var arr2 = [10, 30, 4]
var flag = arr2.some(function (value) {
return value < 3
})
console.log(flag);//false
6、reduce和reduceRight方法
接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值。reduce接受一个函数,函数有四个参数,分别是:上一次的值previousValue,当前值currentValue,当前值的索引index,数组array。
reduceRight方法和reduce方法一样,都是求数组累计数。不同的是reduceRight()从数组的末尾向前将数组中的数组项做累加。
reduceRight()首次调用回调函数callbackfn时,prevValue 和 curValue 可以是两个值之一。如果调用 reduceRight() 时提供了 initialValue 参数,则 prevValue 等于 initialValue,curValue 等于数组中的最后一个值。如果没有提供 initialValue 参数,则 prevValue 等于数组最后一个值, curValue 等于数组中倒数第二个值。
//reduce 累计
//arr.reduce(function(res,val,index){ return },n) res返回的值 n是定义res的初始值
var arr = [1, 2, 3, 45, 3, 2, 6, 4, 7, 4, 1, 2, 7]
var arr = arr.reduce(function (res, item) {
if (res.indexOf(item) == -1) {
res.push(item)
}
// console.log(val);
return res
}, [])//[]定义res的初始值
console.log(arr);// [1, 2, 3, 45, 6, 4, 7] 去重
var arr = [1, 2, 3, 45, 3, 2, 6, 4, 7, 4, 1, 2, 7]
var all = arr.reduce(function (a, b, index, self) {
// console.log(a, b, index, self);
return a + b
})
console.log(all);
7、foreach方法
循环遍历数组的元素,作用相当于for循环,无返回值。
var arr = [1, 2, 3]
var sum = 0
arr.forEach(function (value, index, array) {
console.log('每个数组元素' + value);
console.log('每个数组元素的索引号' + index);
console.log('数组本身' + array);
sum += value
})
console.log(sum);
8、keys,values,entries方法
ES6 提供三个新的方法,entries(),keys()和values(),用于遍历数组。它们都返回一个遍历器对象,可以用for…of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历。
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
9、Array.from静态方法
Array.from()方法主要用于将两类对象(类似数组的对象[array-like object]和可遍历对象[iterable])转为真正的数组
//类似数组的对象转为真正的数组
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
}
console.log(Array.from(arrayLike)); // ["a","b","c"]
//字符转数组
let arr = Array.from('w3cplus.com') //字符转数组
console.log(arr); // ["w","3","c","p","l","u","s",".","c","o","m"]
//Set数据转数组
let namesSet = new Set(['a', 'b']) //new Set创建无重复元素数组
let arr2 = Array.from(namesSet); //将Set结构数据转为数组
console.log(arr2); //["a","b"]
//转换Map数据
let m = new Map([[1, 2], [2, 4], [4, 8]]);
console.log(Array.from(m)); // [[1, 2], [2, 4], [4, 8]]
//接受第二个参数为map转换参数
var arr = Array.from([1, 2, 3]); //返回一个原样的新数组
var arr1 = Array.from(arr, (x) => x * x)
console.log(arr1); // [1, 4, 9]
var arr2 = Array.from(arr, x => x * x);
console.log(arr2); // [1, 4, 9]
var arr3 = Array.from(arr).map(x => x * x);
console.log(arr3); // [1, 4, 9]
//大样本生成
var arr4 = Array.from({length:5}, (v, i) => i);
console.log(arr4); // [0, 1, 2, 3, 4]
10、Set数组对象用法
ES6 提供了新的数据结构 Set。它类似于数组,但是成员的值都是唯一的,没有重复的值
var arr = [12,12,3,45,56,34,23,42,3,4,4,45]
console.log(new Set(arr)); //Set(8)
var arr2 =[...new Set(arr)]
console.log(arr2); //[12,3,45,56,34,23,42,4]
var s1 =new Set([1,88,8,1,3])
//添加
s1.add('love')
//删除
s1.delete(8)
//检测
console.log(s1.has('love')); //true
//长度
console.log('长度',s1.size,s1); //长度 4 Set(4)