Array()
let arr1 = new Array() // 空数组let arr2 = new Array(3) // 指定长度let arr3 = new Array("hello") // 元素
Array.from()
将伪数组转化为数组
Array.from("hello,world")// (11) ["h", "e", "l", "l", "o", ",", "w", "o", "r", "l", "d"]const arrayLikeObject = {0: 1,1: 2,2: 3,3: 4,length: 4 // 必须有length属性};Array.from(arrayLikeObject)(4) [1, 2, 3, 4]// 可以指定map函数和thisconst a1 = [1, 2, 3, 4];const a2 = Array.from(a1, x => x**2);const a3 = Array.from(a1, function(x) {return x**this.exponent}, {exponent: 2});alert(a2); // [1, 4, 9, 16]alert(a3); // [1, 4, 9, 16]
Array.of()
将参数转换为数组
Array.of(1,2,3,4)// (4) [1, 2, 3, 4]Array.of({name:"hello"}, 1, 2, "yes")// (4) [{…}, 1, 2, "yes"]Array.of(...[1,2,3])(3) [1, 2, 3]
Array holes
ES6后和ES6前对待稀疏数组的方式不同,应避免使用稀疏数组
const options = [1,,,,5];// map() will skip the holes entirelyalert(options.map(() => 6)); // [6, undefined, undefined, undefined, 6]// join() treats holes as empty stringsalert(options.join('-')); // "1----5"
判断数组
let arr = [1, 2, 3]arr instanceof Array // 只能用于单页面的global scope// trueArray.isArray(arr) // 可以用于任何环境// true
keys(), values(), entries()
const a = ["foo", "bar", "baz", "qux"];for(let i of a.keys()) console.log(i) // 0 1 2 3for(let value of a.values()) console.log(value) // foo bar baz quxfor(let [key, value] of a.entries()) console.log(key, value) // 0 "foo" 1 "bar" 2 "baz" 3 "qux"
fill(), copyWithin()
fill()给指定位置填充一个数值,原数组会被改变
let a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]a.fill("H")// (10) ["H", "H", "H", "H", "H", "H", "H", "H", "H", "H"]a.fill("H", 3, 4) // 指定开始和结束的index// (10) [0, 1, 2, "H", 4, 5, 6, 7, 8, 9]
copyWithin()复制指定位置到指定位置,原数组会被改变
let a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]a.copyWithin(6) // 第一个参数是替换的起始位置index// (10) [0, 1, 2, 3, 4, 5, 0, 1, 2, 3]a.copyWithin(6, 0, 2) // 第二三参数是复制的开始和结束位置// (10) [0, 1, 2, 3, 4, 5, 0, 1, 8, 9]
join()
将数组转换为字符串
let a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]a.join("+") // 指定连接符号"0+1+2+3+4+5+0+1+2+3"
unshift(),push()
在数组头/尾插入任意多个元素,返回数组长度
let a = []a.push(1,2)// 2a.unshift('a','b')// 4a// (4) ["a", "b", 1, 2]
shift(), pop()
从数组头/尾弹出元素,并返回这个元素
a.shift()// "a"a.pop()// 2
reverse()
sort()
默认先将每个元素转换为字符串,再排序
let a = [15, 10, 1, 2, 25, 3]a.sort()// (6) [1, 10, 15, 2, 25, 3]
sort()接受一个比较函数
a.sort((a,b)=>a-b)// (6) [1, 2, 3, 10, 15, 25]
compareFunction(a, b) 必须总是对相同的输入返回相同的比较结果,否则排序的结果将是不确定的。
concat()
返回原数组和arguments组成的新数组,如果什么参数都不传,则仅复制原数组
let a = ['a']a.concat("hello", [1,2,3], {0:"a",1:"b"})// (6) ["a", "hello", 1, 2, 3, {…}]
可以给数组或对象设置Symbol.isConcatSpreadable属性,指定是否被拍平
let colors = ["red", "green", "blue"];let newColors = ["black", "brown"];let moreNewColors = {[Symbol.isConcatSpreadable]: true, // 默认是falselength: 2,0: "pink",1: "cyan"};newColors[Symbol.isConcatSpreadable] = false; // 默认是true// Force the array to not be flattenedlet colors2 = colors.concat("yellow", newColors);// Force the array-like object to be flattenedlet colors3 = colors.concat(moreNewColors);alert(colors); // ["red", "green","blue"]alert(colors2); // ["red", "green", "blue", "yellow", ["black", "brown"]]alert(colors3); // ["red", "green", "blue", "pink, "cyan"]
slice()
切片,不改变原数组
参数:开始index和结束index(不包含),
如果参数是负数,则加上length
let a = [1,2,3,4,5,6,7]a.slice(3)// (4) [4, 5, 6, 7]a.slice(3,4)// [4]a.slice(-4,-3)// [4]
splice()
参数:起始位置,删除个数,插入元素
会改变数组,返回的是删除的元素
let a = [1, 2, 3, 4, 5, 6, 7]a.splice(1,3,'a','b')// (3) [2, 3, 4] 返回被删元素a// (6) [1, "a", "b", 5, 6, 7]
indexOf(),lastIndexOf(), includes()
都接受一个要寻找的元素,和要开始查找的位置,找不到返回-1或false
let a = [1, 2, 3, 3, 2, 1]a.indexOf(3)// 2a.lastIndexOf(3)// 3a.includes(3)// truea.includes(3, 5)// false
必须精确相等
let person = { name: "Nicholas" };let people = [{ name: "Nicholas" }];let morePeople = [person];people.indexOf(person); // -1morePeople.indexOf(person); // 0people.includes(person); // falsemorePeople.includes(person); // true
find(), findIndex()
都接受一个函数 predicate(element, index, array) , 将函数的返回值作为找寻条件
let a = [1, 2, 3, 3, 2, 1 ]a.find(item => item < 3) // 找到第一个符合条件的元素就不再往后找// 1a.findIndex(item => item < 3)// 0a.find(item => item > 4)// undefineda.findIndex(item => item > 4)// -1
every(), some()
**every()** 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。**some()** 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回一个布尔值。
let a = [1, 2, 3, 4, 5]a.every((item, index, array) => item > 3)// falsea.some((item, index, array) => item > 3)// true
filter(), map()
**filter()** 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。**map()** 方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。
let a = [1,3, 5, 2, 4, 2, 5, 6]a.filter((item, index, array) => item > 3)// (4) [5, 4, 5, 6]a.map((item, index, array)=> item ** 2)// (8) [1, 9, 25, 4, 16, 4, 25, 36]
forEach()
**forEach()** 方法对数组的每个元素执行一次给定的函数。没有返回值,相当于for循环
a.forEach((item,index, array)=> console.log(item**2)) // 1 9 25 4 16 4 25 36
reduce(), reduceRight()
**reduce()** 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值,reduce()接受两个参数,一个function,一个初始值;function接受4个参数,the accumulator value, the current value, the item’s index, and the array object,
如果没给初始值,遍历从第二个元素开始,accumulator是第一个元素,current是第二元素,对一个item执行完函数之后都更新accumulator 的值;
如果给了初始值,遍历从第一个元素开始;
reduceRight()与reduce()的方向相反,从数组末尾开始
let a = [1,2,3,4,5,6]a.reduce((accu,cur,index, array)=> accu + cur)// 21a.reduce((accu,cur,index, array)=> accu + cur, 100)// 121
reduce实现filter
let arr = [1, 2, 3, 4, 5]arr.reduce((result, item)=>{if(item%2 === 1){return result}else{return result.concat(item)}}, [])arr.reduce((result, item)=> result.concat(item%2 === 1 ? [] : item), [])
