Open LLwanran opened 5 years ago
push(), unshift()
var animals = ['pigs', 'goats', 'sheep']; animals.push('cows'); console.log(animals); // expected output: Array ["pigs", "goats", "sheep", "cows"]
unshift:将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)
var array = [1, 2, 3]; array.unshift(4, 5) console.log(array); // expected output: Array [4, 5, 1, 2, 3]
pop(), shift()
var plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; plants.pop(); console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
shift:从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度
var array = [1, 2, 3]; array.shift(); console.log(array); // expected output: Array [2, 3]
join(), concat(), reverse()
var elements = ['Fire', 'Air', 'Water'];
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']
slice()
var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
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']
sort()
var months = ['March', 'Jan', 'Feb', 'Dec']; months.sort(); console.log(months); // expected output: Array ["Dec", "Feb", "Jan", "March"]
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"
Array.from(), Array.of(), isArray()
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.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
find(), findIndex()
var array = [5, 12, 8, 130, 44]; var found = array.find(function(element) { return element > 10; }); console.log(found); // expected output: 12
findIndex:返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1
var array = [5, 12, 8, 130, 44]; function isLargeNumber(element) { return element > 13; } console.log(array.findIndex(isLargeNumber)); // expected output: 3
indexOf(), includes()
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
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
reduce()
var array = [1, 2, 3, 4]; var reducer = (accumulator, currentValue) => accumulator + currentValue;
// 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
push(), unshift()
push:将一个或多个元素添加到数组的末尾,并返回该数组的新长度unshift:将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)
pop(), shift()
pop:从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度shift:从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度
join(), concat(), reverse()
join:将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符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"
reverse:将数组中元素的位置颠倒,并返回该数组。该方法会改变原数组
slice()
返回一个新的数组对象,这一对象是一个由 begin和 end(不包括end)决定的原数组的浅拷贝。原始数组不会被改变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"]
sort()
用原地算法对数组的元素进行排序,并返回数组。排序算法现在是稳定的。默认排序顺序是根据字符串Unicode码点。 由于它取决于具体实现,因此无法保证排序的时间和空间复杂性var array1 = [1, 30, 4, 21, 100000]; array1.sort(); console.log(array1); // expected output: Array [1, 100000, 21, 30, 4]
Array.from(), Array.of(), isArray()
Array.from:从一个类似数组或可迭代对象中创建一个新的,浅拷贝的数组实例console.log(Array.from([1, 2, 3], x => x + x)); // expected output: Array [2, 4, 6]
Array.isArray:用于确定传递的值是否是一个 Array
find(), findIndex()
find:返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefinedfindIndex:返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1
indexOf(), includes()
indexOf:返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1console.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
reduce()
reduce:对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值// 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