JS Array数组方法

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

const array1 = [1, 2, 3];
// 返回数组长度
console.log(array1.unshift(4, 5));// 5
// 原数组改变
console.log(array1);// [4, 5, 1, 2, 3]

2、push() 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。尾部添加(原数组改变)

const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
// 返回数组长度
console.log(count);// 4
// 原数组改变
console.log(animals);//  ["pigs", "goats", "sheep", "cows"]

3、shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。头部删除(原数组改变)

const array1 = [1, 2, 3];

const firstElement = array1.shift();
// 原数组改变
console.log(array1);// [2, 3]
// 返回删除元素
console.log(firstElement);//  1

4、pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。尾部删除(原数组改变)

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
// 返回删除元素
console.log(plants.pop());// "tomato"
// 原数组改变
console.log(plants);//  ["broccoli", "cauliflower", "cabbage", "kale"]

5、concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。拼接

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
// 拼接数组得到新数组
const array3 = array1.concat(array2);
// 新数组
console.log(array3);// ["a", "b", "c", "d", "e", "f"]

6、findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。头部查找(第一个满足条件)返回索引值 ES6

const array1 = [5, 12, 8, 130, 44];

// 元素判断公式
const isLargeNumber = (element) => element > 13;

console.log(array1.findIndex(isLargeNumber));//  3

7、indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。头部查找(第一个满足条件)返回索引值

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];

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

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

8、find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。头部查找(第一个满足条件)返回值 ES6

const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);// 12

9、lastIndexOf() 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。尾部查找(第一个满足条件)返回索引值

const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];

console.log(animals.lastIndexOf('Dodo'));//  3

console.log(animals.lastIndexOf('Tiger'));//  1

10、includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。返回布尔值 ES6

const array1 = [1, 2, 3];

console.log(array1.includes(2));//  true

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

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

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

11、toString() 返回一个字符串,表示指定的数组及其元素。

const array1 = [1, 2, 'a', '1a'];

console.log(array1.toString());// "1,2,a,1a"

12、join() 方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。

const elements = ['Fire', 'Air', 'Water'];

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

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

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

13、slice() 方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。剪切(截取)

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

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

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

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

14、splice() 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。拼接(原数组改变)

第一个参数定义了应添加新元素的位置(拼接)。第二个参数定义应删除多少元素。其余参数定义要添加的新元素

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);//	["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);//  ["Jan", "Feb", "March", "April", "May"]

15、toLocaleString() 返回一个字符串表示数组中的元素。数组中的元素将使用各自的 toLocaleString 方法转成字符串,这些字符串将使用一个特定语言环境的字符串(例如一个逗号 “,”)隔开。

const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
const localeString = array1.toLocaleString('en', { timeZone: 'UTC' });

console.log(localeString);
//  "1,a,12/21/1997, 2:12:00 PM",
// This assumes "en" locale and UTC timezone - your results may vary

16、sort() 方法用原地算法对数组的元素进行排序,并返回数组。默认排序顺序是在将元素转换为字符串,然后比较它们的UTF-16代码单元值序列时构建的,由于它取决于具体实现,因此无法保证排序的时间和空间复杂性。排序(三大排序)ES6

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);//  ["Dec", "Feb", "Jan", "March"]

const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);//  [1, 100000, 21, 30, 4]

17、reverse() 方法将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。该方法会改变原数组。倒序(原数组改变)

const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);//  ["one", "two", "three"]

const reversed = array1.reverse();
console.log('reversed:', reversed);//  ["three", "two", "one"]

console.log('array1:', array1);//  ["three", "two", "one"]

18、flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。扁平化 ES6

const arr1 = [0, 1, 2, [3, 4]];

console.log(arr1.flat());//  [0, 1, 2, 3, 4]

const arr2 = [0, 1, 2, [[[3, 4]]]];

console.log(arr2.flat(2));//  [0, 1, 2, [3, 4]]

19、filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);// ["exuberant", "destruction", "present"]

20、map() 方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);//  [2, 8, 18, 32]

21、forEach() 方法对数组的每个元素执行一次给定的函数

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

//  "a"
//  "b"
//  "c"

22、every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));// true

23、some() 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。

const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));//  true

24、reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
//  10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
//  15

25、reduceRight() 方法接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值。

const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
  (accumulator, currentValue) => accumulator.concat(currentValue)
);

console.log(array1);
//  [4, 5, 2, 3, 0, 1]

26、from() 方法从一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。 ES6

console.log(Array.from('foo'));//  ["f", "o", "o"]

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

27、of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。ES6

Array.of(1, 2, 3); // [1, 2, 3]
Array(1, 2, 3);    // [1, 2, 3]

28、fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。ES6

const array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
//  [1, 2, 0, 0]

// fill with 5 from position 1
console.log(array1.fill(5, 1));
//  [1, 5, 5, 5]

console.log(array1.fill(6));
//  [6, 6, 6, 6]

29、copyWithin() 方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度。ES6

const array1 = ['a', 'b', 'c', 'd', 'e'];

// copy to index 0 the element at index 3
console.log(array1.copyWithin(0, 3, 4));
//  ["d", "b", "c", "d", "e"]

// copy to index 1 all elements from index 3 to the end
console.log(array1.copyWithin(1, 3));
//  ["d", "d", "e", "d", "e"]

30、entries() 方法返回一个新的Array Iterator对象,该对象包含数组中每个索引的键/值对。ES6

const array1 = ['a', 'b', 'c'];

const iterator1 = array1.entries();

console.log(iterator1.next().value);
//  [0, "a"]

console.log(iterator1.next().value);
//  [1, "b"]

31、keys() 方法返回一个包含数组中每个索引键的Array Iterator对象。ES6

const array1 = ['a', 'b', 'c'];
const iterator = array1.keys();

for (const key of iterator) {
  console.log(key);
}

//  0
//  1
//  2

32、values() 方法返回一个新的 Array Iterator 对象,该对象包含数组每个索引的值 ES6

const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
  console.log(value);
}

//  "a"
//  "b"
//  "c"

33、flatMap() 方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。它与 map 连着深度值为1的 flat 几乎相同,但 flatMap 通常在合并成一种方法的效率稍微高一些。ES6

var arr1 = [1, 2, 3, 4];

arr1.map(x => [x * 2]); 
// [[2], [4], [6], [8]]

arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]

// only one level is flattened
arr1.flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]

34、判断是否变量数组

(1)instanceof

[1,2] instanceof Array 
true

(2)Array.isArray()

Array.isArray([1,2])
true

(3)Object.prototype.toString.call(arr) === “[object Array]”

Object.prototype.toString.call([1,2])
"[object Array]"

(4)arr.constructor.name===’Array’

[1,2].constructor.name==='Array';
true

以上内容纯属个人见解,还请多多指教!!!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript中的数组方法用于对数组进行各种操作和转换。下面是一些常见的数组方法: 1. push():将一个或多个元素添加到数组的末尾。 2. pop():移除并返回数组的最后一个元素。 3. shift():移除并返回数组的第一个元素。 4. unshift():将一个或多个元素添加到数组的开头。 5. concat():将两个或多个数组合并为一个新数组。 6. slice():返回选定数组的一部分,不修改原始数组。 7. splice():从数组中添加、删除或替换元素。 8. indexOf():返回指定元素在数组中的第一个匹配位置的索引。 9. lastIndexOf():返回指定元素在数组中最后一个匹配位置的索引。 10. forEach():对数组的每个元素执行提供的函数。 11. map():对数组的每个元素执行提供的函数,并返回新数组。 12. filter():根据指定的条件筛选数组元素,并返回一个新数组。 13. reduce():从左到右对数组的每个元素执行提供的函数,以将其减少为单个值。 14. reduceRight():从右到左对数组的每个元素执行提供的函数,以将其减少为单个值。 15. sort():对数组的元素进行排序。 16. reverse():颠倒数组的元素顺序。 17. join():将数组的所有元素连接成一个字符串。 18. includes():判断数组是否包含某个元素。 19. every():判断数组的每个元素是否都满足指定条件。 20. some():判断数组的某个元素是否满足指定条件。 以上是一些常见的数组方法。你可以根据需要选择适合的方法来操作和处理数组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值