数组,再熟悉不过的数据类型,自己抽时间做一个整理,归类,了解操作API,才能更好运用
大致的整理归类为2种类型: 1) 会修改原来数组 2)不会修改原来数组
会修改原来数组的常用方法
- Array.prototype.pop() 从数组中删除最后一个元素并返回该元素。
pop 删除数组中的最后一个元素并返回该元素。 此方法更改数组的长度
讲讲pop的具体用法及兼容性
var plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']
console.log(plants.pop()); // "tomato“
- Array.prototype.push() 将一个或多个元素添加到数组的末尾并返回数组的长度
let a = [1];
console.log(a.push(2)); //返回数组长度, 会改变a的原始长度
// 元素可以累添加; a.push(3,4,5,6);
console.log(a) // [1,2];
- Array.prototype.sort() 对数组的元素进行排序并返回数组。
arr.sort([compareFunction[firstE1, secondE2]])
compareFunction 比较的方法, 指定定义排序顺序的函数。
如果省略,则将数组元素转换为字符串,然后根据每个字符的Unicode代码点值进行排序。
let numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
return a - b;
});
console.log(numbers); [1, 2, 3, 4, 5]
- Array.prototype.shift() 从数组中删除第一个元素并返回该元素。
返回值: 从数组中删除的元素; undefined
如果数组为空, 否则返回删除的元素值
会改变原数组。
// 该shift()方法从数组中删除第一个元素并返回已删除的元素。此方法更改数组的长度。
let array1 = [1, 2, 3];
let firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
- Array.prototype.splice() 对数组的元素进行排序并返回数组
通过删除或替换现有元素和/或在适当位置添加新元素来更改数组的内容。
array .splice(start [,deleteCount [,item1 [,item2 [,…]]]])
start: 要开始更改数组的索引,如果大于数组的长度,start
则将设置为数组的长度
deleteCount: 一个整数,指示要从中删除的数组中的元素个数。
item1, item2… 要添加到数组的元素,从 start
。如果未指定任何元素,splice()
则只会从数组中删除元素。
返回值:包含已删除元素的数组。如果仅删除一个元素,则返回一个元素的数组。如果未删除任何元素,则返回空数组。
let months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb'); // 没有删除元素,则返回一个空数组 []
// inserts at index 1
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
- Array.prototype.unshift()() 将一个或多个元素添加到数组的前面,并返回数组的新长度 返回值: 返回数组的长度 ```javascript let array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // expected output: 5
console.log(array1); // expected output: Array [4, 5, 1, 2, 3]
7. Array.prototype.copyWithin() 复制数组中的一系列数组元素 ()
8. Array.prototype.fill() 使用静态值从开始索引到结束索引填充数组的所有元素。
不会修改数组并返回数组的某些表示形式<br />arr .fill(value [,start [ ,end]])f<br />value: 填充数组的值。<br />start: 启动索引,默认为0。<br />end: 结束索引,默认为`this.length`。
```javascript
[1, 2, 3].fill(4); // [4, 4, 4]
[1, 2, 3].fill(4, 1); // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2); // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1); // [1, 2, 3]
[1, 2, 3].fill(4, 3, 3); // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2); // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN); // [1, 2, 3]
[1, 2, 3].fill(4, 3, 5); // [1, 2, 3]
Array(3).fill(4); // [4, 4, 4]
[].fill.call({ length: 3 }, 4); // {0: 4, 1: 4, 2: 4, length: 3}
不会修改数组长度
- Array.prototype.concat() 返回一个新数组,该数组是与其他数组和/或值连接的数组。
返回值: 一个新的Array
实例。
const letters = ['a', 'b', 'c'];
const numbers = [1, 2, 3];
letters.concat(numbers);
// result in ['a', 'b', 'c', 1, 2, 3]
- Array.prototype.reverse() 反转数组元素的顺序 - 第一个成为最后一个,最后一个成为第一个。
返回值: 反转后的对象
let a = [1,2,3];
console.log(a.reverse()); //得到的是原来数组的值 [3,2,1]
console.log(a); // [1,2,3]
Array.prototype.indexOf() 返回数组中元素的第一个(最小)索引,该索引等于指定的值,如果找不到,则返回-1。
Array.prototype.join() 将数组的所有元素连接成一个字符串。
- Array.prototype.lastIndexOf() 返回数组中元素的最后一个(最大)索引,该索引等于指定值,如果找不到,则返回-1。
- Array.prototype.slice() 提取数组的一部分并返回一个新数组。
arr .slice([ begin [,end ]])
begin:
从零开始的索引,开始提取。可以使用负索引,指示距序列末尾的偏移量。slice(-2)
提取序列中的最后两个元素。如果begin
未定义,slice
则从索引开始0
。如果begin
大于序列的长度,则返回空数组
end
基于零的索引,在此之前结束提取。slice
提取但不包括end
。例如,slice(1,4)
通过第四个元素(元素索引为1,2和3)提取第二个元素。可以使用负索引,指示距序列末尾的偏移量。slice(2,-1)
通过序列中倒数第二个元素提取第三个元素。如果end
省略,则slice
提取序列的末尾(arr.length
)。如果end
大于序列的长度,则slice
提取到序列的末尾(arr.length
)。
返回值: 包含提取元素的新数组。
const fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
const citrus = fruits.slice(1, 3);
// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
Array.prototype.toSource() 返回表示指定数组的数组文字; 您可以使用此值来创建新数组
Array.prototype.toString() 返回表示数组及其元素的字符串。
const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// expected output: "1,2,a,1a"
- Array.prototype.toLocalString() 返回表示数组及其元素的本地化字符串。