1. find()
返回满足条件的数组的第一个元素的值
array.find(function(value, index, arr),thisValue)
value:必须,代表当前元素,其他四个参数都是可选,index代表当前索引值,arr代表当前的数组,thisValue代表传递给函数的值,一般用this值,如果这个参数为空,undefined会传递给this值
返回值:返回符合测试条件的第一个数组元素的值,如果没有符合条件的则返回undefined。
var list = []
var item = list.find(e => e.key === appId)
if (item) return item.name
2. findIndex()
方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
3. filter()
创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素
array.filter(function(value, index, arr),thisValue)
value:必须,代表当前元素,其他四个参数都是可选,index代表当前索引值,arr代表当前的数组,thisValue代表传递给函数的值,一般用this值,如果这个参数为空,undefined会传递给this值
返回值:返回数组,包含了符合条件的所有元素,如果没有符合条件的则返回空数组
cTypeIds () {
return this.typeIdListData.filter((item, index) => {
return this.typeIds.includes('' + item.typeId)
})
},
4. map()
返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,map()方法按照原始数组元素顺序依次处理元素
array.map(function(value, index, arr),thisValue)
this.areadata.prov = provinceList.map(v => {
return { id: v.id, name: v.shortName }
})
5. forEach()
方法对数组的每个元素执行一次给定的函数;
注意没有办法跳出或终止forEach语句,除非抛出异常。
array.forEach(function(value, index, arr),thisValue)
forEach()返回值为undefined,里面即便有return语句,返回值依然是undefined
res.result.data.forEach(element => {
element.addTime = moment(element.addTime).format('YYYY-MM-DD HH:mm')
})
const dataIncome = []
const dataOutlay = []
data.forEach(item => {
const pn = this.getNameByKey(item.product)
dataIncome.push({ name: pn, count: item.income, percent: item.incomeRate })
dataOutlay.push({ name: pn, count: item.outlay, percent: item.outlayRate })
})
6. every()
测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值
注意:若收到一个空数组,此方法在一切情况下都会返回 true。
const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// expected output: true
7. fill()
fill()方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。
const array1 = [1, 2, 3, 4];
// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]
// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]
console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]
8. flat()
方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个.
var newArray = arr.flat([depth])
#指定要提取嵌套数组的结构深度,默认值为 1
var arr1 = [1, 2, [3, 4]];
arr1.flat();
// [1, 2, 3, 4]
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
9. flatMap()
方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。它与 map 连着深度值为1的 flat 几乎相同,但 flatMap 通常在合并成一种方法的效率稍微高一些。
var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
// return element for new_array
}[, thisArg])
let arr1 = ["it's Sunny in", "", "California"];
arr1.map(x => x.split(" "));
// [["it's","Sunny","in"],[""],["California"]]
arr1.flatMap(x => x.split(" "));
// ["it's","Sunny","in", "", "California"]
10. includes()
方法用来判断一个数组是否包含一个指定的值,如果包含则返回 true,否则返回false。
使用 includes()比较字符串和字符时是区分大小写。
arr.includes(valueToFind[, fromIndex])
fromIndex 可选
从fromIndex 索引处开始查找 valueToFind。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜 (即使从末尾开始往前跳 fromIndex 的绝对值个索引,然后往后搜寻)。默认为 0。
11. slice()
方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
12. join()
方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。
如果一个元素为 undefined 或 null,它会被转换为空字符串。
> arr.join([separator])
> separator如果缺省该值,数组元素用逗号(,)分隔
var a = ['Wind', 'Rain', 'Fire'];
var myVar1 = a.join(); // myVar1的值变为"Wind,Rain,Fire"
var myVar2 = a.join(', '); // myVar2的值变为"Wind, Rain, Fire"
var myVar4 = a.join(''); // myVar4的值变为"WindRainFire"
reduce()
方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
//无初始值,结果10
[0, 1, 2, 3, 4].reduce((prev, curr) => prev + curr );
//初始值10,结果20
[0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => {
return accumulator + currentValue
}, 10)