新建数组
new Array()
[1,2,3]
Array.of(1,2,3)
Array.of()是ES6中新增的将一组值转换为数组的方法,该方法的出现时为了弥补构造函数Array()因为参数不同导致的不同行为
| new Array() ==== []
new Array(1) ==== []
new Array(1,2) ==== [1,2] | Array.of() === []
Array.of(1) === [1]
Array.of(1,2) === [1,2] |
| —- | —- |
new Array()只有在参数个数不少于2时候,才会返回新的数组
检测是否为数组
isArray
Object.prototype.toString.call(arr)===’[object Array]’
更改原数组的api
| 添加/删除 | 添加
- push()
- 末尾添加,返回新数组长度
- unshift()
- 开始添加,返回数组长度
删除
- pop()
- 尾项删除,返回移除的项
- shift()
- 首项删除,返回移除的项
|
| —- | —- |
| 排序 | reverse()
- 反转数组顺序
sort()
- 默认按字符串比较(调用toString()方法)
```
//es5
data.sort(function(a,b){
return a-b;
});
//es6
data.sort((a,b)=>a-b)
|| splice() | <br />- 用于插入、删除或替换数组的元素<br />- splice(start,howmany,item1,item2...)<br />- start从何处删,howmany删除几个若无则删除全部,之后的元素是预添加的新元素(从删除位置添加)<br /> || copyWithin() | arr.copyWithin(target[, start[, end]])<br />- 从目标元素开始复制start到end索引对应的指定元素,若end为空,则复制到数组结束为止。<br /> |<a name="eR50u"></a># 不更改原数组生成新数组的api| 截取 | slice<br />- slice(start,end)提取index∈[start,end)的元素<br />- 浅拷贝,生成新数组<br /> || --- | --- || 连接 | concat<br />- let arr = [].concat.apply([], arguments); //没有去重复的新数组<br /> || 寻找 | find<br />- 返回数组中满足提供的测试函数的第一个元素的值<br />includes<br />- arr.includes(searchElement, fromIndex)<br />- 返回boolean<br />indexOf<br />- 在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1<br /> || 迭代 | <br />- map<br />- filter<br />- forEach<br />- some<br />- every<br /> || 归并<br />- function(prev,cur,index,array)<br /> | <br />- reduce<br /> - reduce(function(accumulator, currentValue, currentIndex, array),initialValue)<br /> - accumulator累加器累加回调的返回值; 它是上一次调用回调时返回的累积值<br />- reduceRight<br /> |<a name="2aan9"></a># 数组的迭代方法总结[语雀内容](https://www.yuque.com/u2728/code-basis/gop57r?view=doc_embed)<a name="nCAPs"></a># 复杂api细讲<a name="65qYd"></a>## Array.splice()> Array.splice(begin, deleteCount, addItem1, addItem2...)用法:1. splice(begin, deleteCount) 表示从 begin 索引的位置开始,删除deleteCount个元素1. splice(begin, 0, ele1, ele2, ...) 表示从 begin索引的元素后面插入一系列的元素1. splice(begin, deleteCount, ele1, ele2, ...) 表示从 begin索引的位置开始,删除 count 个元素,然后再插入一系列的元素注意点:- 返回值为`被删除元素`组成的`数组`。- 改变原始数组```javascriptconst array = [1, 2, 3, 4, 5];// console.log(array.splice(1,2)); // return [2,3] 原数组变成 [1,4,5]// console.log(array.splice(1, 0, 'hahah', 'aaaa')); // return [] 原数组 变成 [1,'hahah', 'aaaa', 2, 3, 4, 5]console.log(array.splice(1, 3, 'hahah', 'aaaa')); // return [2,3,4] 插入 [1,5] 插入后=> [1, 'hahah', 'aaaa', 5]
Array.slice()
Array.slice([startIndex [, endIndex]]) [startIndex , endIndex) 不包含endIndex
不改变原数组,创建新数组
用法:
- arr.slice() 浅拷贝数组
- arr.slice(2,5) 截取数组中index=2、index=3、index=4的元素生成新数组
- arr.slice(-2,5) starIndex为负,则从len+starIndex位置开始复制,此时endIndex只能是比 start 大的负数,否则将返回空数组。
const array = ["one", "two", "three","four", "five"];
console.log(array.slice()); // ["one", "two", "three","four", "five"]
console.log(array.slice(2,3)); // ["three"]
console.log(array.slice(-4)); // ["two", "three","four", "five"]
console.log(array.slice(-4, -2)); // ["two", "three"]
Array.reduce()
核心用法在于:
way1:迭代的初始值是迭代的初始启动值,在迭代的过程中是累加值得到传递,最后需要的是这个累加值
典型应用:数组累加、数组最大最小值
way2、迭代的初始值为容器,迭代的累加过程是对这个容器进行改造,迭代自始至终都是维护的这个容器的改变,典型应用:数组转对象、成员个数统计
# reduce的way1使用-数组的累加
function Accumulation(...vals) {
return vals.reduce((t, v) => t + v, 0);
}
# reduce的way1使用-数组的最大值
function Min(arr = []) {
return arr.reduce((t, v) => t < v ? t : v));
}
==========
# reduce的way2使用-数组转对象
const people = [
{ area: "GZ", name: "YZW", age: 27 },
{ area: "SZ", name: "TYJ", age: 25 }
];
const map = people.reduce((t, v) => {
const { name, ...rest } = v;
t[name] = rest;
return t;
}, {}); // { YZW: {…}, TYJ: {…} }
# 数组成员个数统计
const arr = [0, 1, 1, 2, 2, 2];
console.log(getCount(arr)); // { 0: 1, 1: 2, 2: 3 }
function getCount(arr) {
return arr.reduce((accurmHash, cur) => {
accurmHash[cur] = (accurmHash[cur] || 0) + 1;
return accurmHash;
}, {});
}
Array.from()
Array.from(arrayLike[, mapFunction[, thisArg]])
- arrayLike:必传参数,想要转换成数组的伪数组对象或可迭代对象。
- mapFunction:可选参数,
mapFunction(item,index){...}是在集合中的每个项目上调用的函数。返回的值将插入到新集合中。 - thisArg:可选参数,执行回调函数
mapFunction时 this 对象。这个参数很少使用。
主要作用:
- 类数组转数组 map.set(‘one’, 1) map.set(‘two’, 2); Array.from(map); // => [[‘one’, 1], [‘two’, 2]]
- 数组的浅拷贝 arr2 = Array.from(arr1);
- 填充数组 new Array(length).fill(initnum); 等效于 Array.from({ length }, () => init);
当填充多维时Array.from更管用
# 二维矩阵的生成
let mark = new Array(n+1).fill([]);
# 实际上fill的那个数组 是 指向一个地址
解决办法:=> 使用Array.from({ n+1 }, () => ([]));
4.Array.from(new Set(array)) 数组去重
最后
数组相关练习的代码实践:这里
- 数组的常见封装函数锦集: 求数组交集、去重、扁平化数组等
- 数组原型方法的模拟实现源码: map、filter、reduce等
