- join():返回值为用指定的分隔符将数组每一项拼接的字符串
- push():向数组的末尾添加元素,返回值是当前数组的length(修改原数组)
- pop():删除数组末尾的元素,返回值是被删除的元素(修改原数组)
- shift():删除首位元素,返回值是被删除的元素(修改原数组)
- unshift():向首位添加元素,返回值是数组的长度(修改原数组)
- slice():返回数组中指定起始下标和结束下标之间的(不包含结束位置)元素组成的新数组
- splice():对数组进行增删改(修改原数组)
- fill():使用特定值填充数组中的一个或多个元素(修改原数组)
- filter():过滤,数组中的每一项运行给定函数,返回满足过滤条件组成的数组
- concat():用于连接两个或多个数组
- indexOf():返回当前值在数组中第一次出现位置的索引
- lastIndexOf():返回查找的字符串最后出现的位置,如果没有找到匹配字符串则返回 -1。
- every():判断数组中每一项是否都符合条件
- some():判断数组中是否存在满足的项
- includes():判断一个数组是否包含指定的值
- sort():对数字的元素进行排序(修改原数组)
- reverse():对数组进行倒叙(修改原数组)
- forEach():循环遍历数组每一项(没有返回值)
- map():循环遍历数组的每一项(有返回值)
- copyWithin(): 从数组的指定位置拷贝元素到数组的另一个指定位置中(修改原数组)
- find(): 返回第一个匹配的值,并停止查找
- findIndex(): 返回第一个匹配值的索引,并停止查找
- toLocaleString()、toString():将数组转换为字符串
- flat()、flatMap():扁平化数组
- entries() 、keys() 、values():遍历数组
- reduce() reduce的介绍及用法_尼克_张的博客-CSDN博客_.reduce博主总结的reduce各种用法(去重,合并,求和,二维转一维)
1. Array.push(),向数组的末尾添加一个或多个元素,并返回新的数组长度。原数组改变。
var arr = [1,2,6,4,8,5]; arr.push(5,6,7)
2. Array.pop(),删除并返回数组的最后一个元素,若该数组为空,则返回undefined。原数组改变。
var arr = [1,2,6,4,8,5]; var del= arr.pop()
3. Array.unshift(),向数组的开头添加一个或多个元素,并返回新的数组长度。原数组改变。
var arr = [1,2,6,4,8,5]; var res= arr.unshift(8)
4. Array.shift(),删除数组的第一项,并返回第一个元素的值。若该数组为空,则返回undefined。原数组改变。
var arr = [1,2,6,4,8,5]; var res= arr.shift(8)
5. Array.concat(arr1,arr2…),合并两个或多个数组,生成一个新的数组。原数组不变。
6. Array.join(),将数组的每一项用指定字符连接形成一个字符串。默认连接字符为 “,” 逗号。
7. Array.reverse(),将数组倒序。原数组改变。
8. Array.sort(),对数组元素进行排序。按照字符串UniCode码排序,原数组改变。
①从小到大
②从大到小
③按照数组对象中的某个值进行排序
9.Array.map(function),原数组的每一项执行函数后,返回一个新的数组。原数组不变。(注意该方法和forEach的区别)。
10.Array.slice() 按照条件查找出其中的部分内容
参数:
array.slice(n, m),从索引n开始查找到m处(不包含m)
array.slice(n) 第二个参数省略,则一直查找到末尾
array.slice(0)原样输出内容,可以实现数组克隆
array.slice(-n,-m) slice支持负参数,从最后一项开始算起,-1为最后一项,-2为倒数第二项
返回值:返回一个新数组
是否改变原数组:不改变
11.Array.splice(index,howmany,arr1,arr2…) ,用于添加或删除数组中的元素。从index位置开始删除howmany个元素,并将arr1、arr2…数据从index位置依次插入。howmany为0时,则不删除元素。
原数组改变。
12.Array.forEach(function),用于调用数组的每个元素,并将元素传递给回调函数。原数组不变。(注意该方法和map的区别,若直接打印Array.forEach,结果为undefined)。
13.Array.filter(function),过滤数组中,符合条件的元素并返回一个新的数组。
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter(word => word.length > 6); console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
14.Array.every(function),对数组中的每一项进行判断,若都符合则返回true,否则返回false。
15.Array.some(function),对数组中的每一项进行判断,若都不符合则返回false,否则返回true。
16.Array.reduce(function),reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
17.indexOf()
检测当前值在数组中第一次出现的位置索引
参数:array.indexOf(item,start) item:查找的元素 start:字符串中开始检索的位置。
返回值:第一次查到的索引,未找到返回-1。
是否改变原数组:不改变。
11、includes()
判断一个数组是否包含一个指定的值
参数:指定的内容
返回值:布尔值
是否改变原数组:不改变。
接着面试官可能还会问你:
原数组改变的方法有:push pop shift unshift reverse sort splice
不改变原数组的方法有:concat map filter join every some indexOf slice forEach
12、Array.prototype.copyWithin()
const array1 = ['a', 'b', 'c', 'd', 'e']; // copy to index 0 the element at index 3 console.log(array1.copyWithin(0, 3, 4)); // expected output: Array ["d", "b", "c", "d", "e"] // copy to index 1 all elements from index 3 to the end console.log(array1.copyWithin(1, 3)); // expected output: Array ["d", "d", "e", "d", "e"]
13、Array.prototype.entries()
const array1 = ['a', 'b', 'c']; const iterator1 = array1.entries(); console.log(iterator1.next().value); // expected output: Array [0, "a"] console.log(iterator1.next().value); // expected output: Array [1, "b"] //迭代索引和元素 const a = ["a", "b", "c"]; for (const [index, element] of a.entries()) { console.log(index, element); } //使用 for...of 循环 const array = ["a", "b", "c"]; const arrayEntries = array.entries(); for (const element of arrayEntries) { console.log(element); } // [0, 'a'] // [1, 'b'] // [2, 'c']
在非数组对象上调用 entries()
const arrayLike = { length: 3, 0: "a", 1: "b", 2: "c", }; for (const entry of Array.prototype.entries.call(arrayLike)) { console.log(entry); } // [ 0, 'a' ] // [ 1, 'b' ] // [ 2, 'c' ]
14、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]
15、Array.from()
console.log(Array.from('foo')); // expected output: Array ["f", "o", "o"] console.log(Array.from([1, 2, 3], x => x + x)); // expected output: Array [2, 4, 6]
Array.from('foo'); // [ "f", "o", "o" ]
const set = new Set(['foo', 'bar', 'baz', 'foo']); Array.from(set); // [ "foo", "bar", "baz" ]
const map = new Map([[1, 2], [2, 4], [4, 8]]); Array.from(map); // [[1, 2], [2, 4], [4, 8]] const mapper = new Map([['1', 'a'], ['2', 'b']]); Array.from(mapper.values()); // ['a', 'b']; Array.from(mapper.keys()); // ['1', '2'];
Array.from([1, 2, 3], x => x + x); // [2, 4, 6] Array.from({length: 5}, (v, i) => i); // [0, 1, 2, 3, 4]
function combine(){ let arr = [].concat.apply([], arguments); //没有去重复的新数组 return Array.from(new Set(arr)); } var m = [1, 2, 2], n = [2,3,3]; console.log(combine(m,n));