LLwanran / front_end_studying

前端知识要点
https://llwanran.github.io/front_end_studying/
2 stars 1 forks source link

javascript数组之常用方法 #42

Open LLwanran opened 5 years ago

LLwanran commented 5 years ago

unshift:将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)

var array = [1, 2, 3];
array.unshift(4, 5)
console.log(array);
// expected output: Array [4, 5, 1, 2, 3]

shift:从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度

var array = [1, 2, 3];
array.shift();
console.log(array);
// expected output: Array [2, 3]

console.log(elements.join()); // expected output: "Fire,Air,Water"

console.log(elements.join('')); // expected output: "FireAirWater"

console.log(elements.join('-')); // expected output: "Fire-Air-Water"


concat:用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组
```js
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
console.log(array1.concat(array2));
// expected output: Array ["a", "b", "c", "d", "e", "f"]

reverse:将数组中元素的位置颠倒,并返回该数组。该方法会改变原数组

var array = ['one', 'two', 'three'];
var reversed = array.reverse();
console.log(reversed);
// expected output: Array ['three', 'two', 'one']

console.log(animals.slice(2)); // expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4)); // expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5)); // expected output: Array ["bison", "camel", "duck", "elephant"]


*   `splice()`
通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组
```js
var 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']

var array1 = [1, 30, 4, 21, 100000]; array1.sort(); console.log(array1); // expected output: Array [1, 100000, 21, 30, 4]


*   `toString()`
返回一个字符串,表示指定的数组及其元素
```js
var array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// expected output: "1,2,a,1a"

console.log(Array.from([1, 2, 3], x => x + x)); // expected output: Array [2, 4, 6]


Array.of:创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型
Array.of() 和 Array 构造函数之间的区别在于处理整数参数:Array.of(7) 创建一个具有单个元素 7 的数组,而 Array(7) 创建一个长度为7的空数组(注意:这是指一个有7个空位(empty)的数组,而不是由7个undefined组成的数组)
```js
Array.of(7);       // [7]
Array.of(1, 2, 3); // [1, 2, 3]

Array(7);          // [ , , , , , , ]
Array(1, 2, 3);    // [1, 2, 3]

Array.isArray:用于确定传递的值是否是一个 Array

Array.isArray([1, 2, 3]);
// true
Array.isArray({foo: 123});
// false
Array.isArray("foobar");
// false
Array.isArray(undefined);
// false

findIndex:返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1

var array = [5, 12, 8, 130, 44];
function isLargeNumber(element) {
  return element > 13;
}
console.log(array.findIndex(isLargeNumber));
// expected output: 3

console.log(beasts.indexOf('bison')); // expected output: 1

// start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output: 4

console.log(beasts.indexOf('giraffe')); // expected output: -1


includes:用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false
```js
var array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

var pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false

// 1 + 2 + 3 + 4 console.log(array.reduce(reducer)); // expected output: 10

// 5 + 1 + 2 + 3 + 4 console.log(array.reduce(reducer, 5)); // expected output: 15