创建一个Array,打印出它的prototype,可以看到Array自带很多方法。
let arr = [1,2,3];
console.log(arr.__proto__);
//我们这挑几个常用的扒一扒:
// concat / entries / every / fill / filter / find / findIndex / flat / from / join / reducer / soft / splice
concat():创建并返回一个新数组,每个参数的顺序依次是该参数的元素(如果参数是数组)或参数本身(如果参数不是数组)。它不会递归到嵌套数组参数中。
语法:var newArray = oldArray1.concat(oldArray2,[value1,value2])
let arr1 = [1,[2]];
let arr2 = [[5],[6]];
let arr3 = [3,4,arr2];
let newArr = arr1.concat(arr3) arr2[1] = 7 console.log(newArr)
// result in [1,[2],3,4,[5],7]
// 由于arr2 的第二个值被修改为7,生成的新数组,会跟谁指向的引用数据修改而一起改动.
entries():返回了一组可迭代的对象,并增加迭代完成的属性。
keys():返回了一组可迭代的key,并增加迭代完成的属性。
values():返回了一组可迭代value,并增加迭代完成的属性。
// 这里只讲一下entries,另外两个差不多
const array1 = ['a', 'b', 'c'];
const iterator1 = array1.entries();
console.log(iterator1.next());
console.log(iterator1.next());
console.log(iterator1.next());
console.log(iterator1.next());
// result in: // > Object { value: Array [0, "a"], done: false }
// > Object { value: Array [1, "b"], done: false }
// > Object { value: Array [2, "c"], done: false }
// > Object { value: undefined, done: true }
// 可以看出返回了一组可迭代的对象,除了value还有 一个为“done”的字段,用来判断迭代是否完成
every():接收一个回调函数,callback(element,index,array),为数组的每个元素,进行一次遍历,直到返回找个不符合回调函数的值,返回false,终止函数。常用来检查数组中是否有不符合规定的值
some():同上,找一个符合回调函数的值则返回true
function isBelow40(currentValue, index, array) {
console.log(currentValue,index,array);
return currentValue < 40;
}
const array1 = [1, 30, 55, 29, 13];
console.log(array1.every(isBelow40));
// result in :
// > 1 0 Array [1, 30, 55, 29, 13]
// > 30 1 Array [1, 30, 55, 29, 13]
// > 55 2 Array [1, 30, 55, 29, 13]
// > false
// 如果去掉55,则会返回 true
fill():方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。fill(value,start,end)
let arr = [1,2,3];
console.log(arr.fill(4, 1, 2))
//先来个正常的: result in :[1, 4, 3]
console.log(arr.fill(4))
//start 默认为0,end默认为length: result in :[4, 4, 4]
console.log(arr.fill(4, -3, 2))
//当start < 0; end > 0 的时候,默认为0, result in :[4, 4, 3]
console.log([1, 2, 3,5,5,6,7,8,8,8].fill(4, -5, -2))
//当start < end <0,逆序列替换 result in :[1, 2, 3, 5, 5, 4, 4, 4, 8, 8]
console.log(arr.fill(4, NaN, NaN))
// 找不到要替换的片段
Array(3).fill({});
// result in :[{}, {}, {}]
filter():接收一个回调函数,callback(element,index,array),为数组的每个元素,进行一次遍历,筛选出符合回调函数的元素并返回新数组,如果没有任何数组元素通过测试,则返回空数组
//由于方法和every()差不多,只是返回有差异,我们这边直接拿every()的例子来用。便于对比
function isBelow40(currentValue, index, array) {
return currentValue < 40;
}
const array1 = [1, 30, 55, 29, 13];
console.log(array1.filter(isBelow40));
// result in : [1, 30, 29, 13]
find() / findIndex() :接收一个回调函数,callback(element,index,array),把数组的每个元素,进行一次遍历,直到找到第一个符合回调函数条件的元素/(findIndex会返回该元素的下标),否则返回undefined
//由于方法和every()差不多,只是返回有差异,我们这边直接拿every()的例子来用。便于对比
function isBelow40(currentValue, index, array) {
return currentValue > 40;
// 条件稍作修改
}
const array1 = [1, 30, 55, 29, 13];
console.log(array1.find(isBelow40));
// result in : 55
console.log(array1.findIndex(isBelow40));
// result in : 2
flat():flat(depth),depth默认为1,可选择正整数/Infinity
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// result in: [1, 2, 3, 4, [5, 6]]
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// result in: [1, 2, 3, 4, 5, 6]
//使用 Infinity,可展开任意深度的嵌套数组
var arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr4.flat(Infinity);
// result in: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
mapFlat():映射每个元素,并进行depth为1的扁平化,返回一个新的数组
var arr1 = [1, 2, [3, 4],[[5]]];
console.log(arr1.map(x => [x * 2]));
// result in:[ [2], [4], [NaN], [10]]
console.log(arr1.flatMap(x => [x * 2]));
// result in:[2, 4, NaN, 10]
// 与map对比,进行了一层扁平化处理,而且是先遍历,再扁平化,于是[3,4]并不能正确的执行回调函数
console.log(arr1.flatMap(x => [[x * 2]]));
// result in:[ [2], [4], [NaN], [10]]
// depth 为 1
forEach(callback(currentValue,index,arr)):遍历每个元素,返回值undefined
map(callback(currentValue,index,arr)):遍历每个元素,返回值一个新数组
currentValue:数组中正在处理的当前元素(必填)
index:数组中正在处理的当前元素的索引(选填)
arr:`forEach()` 方法正在操作的数组 (选填)
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// result in:a ,b ,c
Array.from():简单而实际的用法就是,把类数组(arrayLike 转化成真数组)
// 类数组,可以通过Set,Map等方法生成,这边以Set为例
const set = new Set(['foo', 'bar', 'baz', 'foo']);
console.log(set)
const setAfter = Array.from(set);
console.log(setAfter)
// result in :查看一下打印,可以看出arrayLike 和 Array 还是有本质区别的
// 首先是键值对
// 有size而不是length
// 所自带的函数也与Array完全不同
includes():检测数组中是否包含某个值,返回true/false,
indexOf():检测数组中是否包含某个值,返回index/-1,
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// result in :true
console.log(array1.indexOf(2));
// result in :1
console.log(array1.indexOf(5));
// result in :-1
join():将数组/类数组的元素拼接,并返回字符串
// 接收一个参数separator:默认为 `,`
const elements = ['yan', 'shao', 'lian'];
cosnt element = [single]
console.log(element.join())
// result in : single ,由于只有一个元素,则不会用上拼接符号
console.log(elements.join());
// result in: "yan,shao,lian" ,默认的separator = `,`
pop():从数组中删除最后一个元素,并返回该元素的值
push():从数组中最后添加一个元素,并返回该数组的长度
shift():从数组中删除第一个元素,并返回该元素的值
unshift():从数组中前面添加一个元素,并返回该数组的长度
const name = ['yan', 'shao', 'lian'];
console.log(name.pop(),name);
// result in :"lian", ["yan", "shao"]
console.log(name.push('newLian'),name)
// result in 3 , ["yan", "shao", "newLian"]
console.log(name.shift(),name);
// result in :"yan", [ "shao" ,"newLian"]
console.log(name.push('newYan'),name)
// result in 3 , ["newYan", "shao", "newLian"]
reducer():遍历数组,返回汇总结果
// params:Accumulator 累加器
// :CurrentValue 当前值
// :initialData (选填),没有值时,默认取index=0为initialData
const array1 = [1, 2, 3, 4];
const reducer = (previousValue, currentValue) =>
{
console.log(previousValue,currentValue)
return previousValue + currentValue;
}
console.log(array1.reduce(reducer,100));
// result in: => 100, 1 => 101, 2 => 103, 3 => 106, 4 => 110
soft():对数组元素进行排序,并返回新数组
// params:compareFunction(element1,element2),需要传入一个排序规则,当return true 倒序/ 当return false 正序
const array1 = [1, 30, 4, 21, 100000];
array1.sort( (m,n) => {
console.log(m,n)
console.log(array1);
return m-n
});
console.log(array1);
// result in :从打印结果我们能看,他对比的过程,并且中间没有改变数组的排序结果,而是标记后再最后返回排序结果
//> 30 1 > Array [1, 30, 4, 21, 100000]
//> 4 30 > Array [1, 30, 4, 21, 100000]
//> 4 30 > Array [1, 30, 4, 21, 100000]
//> 4 1 > Array [1, 30, 4, 21, 100000]
//> 21 4 > Array [1, 30, 4, 21, 100000]
//> 21 30 > Array [1, 30, 4, 21, 100000]
//> 100000 21 > Array [1, 30, 4, 21, 100000]
//> 100000 30 > Array [1, 30, 4, 21, 100000]
//> Array [1, 4, 21, 30, 100000]
splice():对当前数组进行替换,插入,删除
// params:start, 从下标为start的地方开始添加
// 1.start>0 并且大于数组长度,则从后面添加
// 2.start<0 则往后面开始逆序数,当 |start| >数组长度,则默认从0开始插入
// :deleteCount, 要删除的项数量
// :addItem 要增加/替换的项 (选填)
const numbers = [1,2,3,4];
numbers.splice(1, 1, 'new2');
console.log(numbers);
// result in: [1, "new2", 3, 4]
numbers.splice(-10, 0, 'zero');
console.log(numbers);
// result in: ["zero", 1, "new2", 3, 4]