美文网首页
数组的操作方法

数组的操作方法

作者: 于美美 | 来源:发表于2022-08-31 22:36 被阅读0次

1. push():将一个或多个元素添加到数组的末尾,并返回该数组的新长度

const animals = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');

console.log(count); // output: 4
console.log(animals); // output: Array ["pigs", "goats", "sheep", "cows"]

2. pop():从数组中删除最后一个元素,并返回该元素的值。此方法会更改数组的长度

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];

console.log(plants.pop()); // output: "tomato"
console.log(plants); // output: Array ["broccoli", "cauliflower", "cabbage", "kale"]

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

const array1 = [1, 2, 3];
const firstElement = array1.shift();

console.log(array1); // output: Array [2, 3]
console.log(firstElement); // output: 1

4. unshift():将一个或多个元素添加到数组的开头,并返回该数组的新长度

const array1 = [1, 2, 3];

console.log(array1.unshift(4, 5)); // output: 5
console.log(array1); // output: Array [4, 5, 1, 2, 3]

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

6. slice():返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。如果该参数为负数,则表示从原数组中的倒数第几个元素开始提取 。arr.slice([begin[, end]])

7. sort():排序

8. concat():合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);

console.log(array3); // output: Array ["a", "b", "c", "d", "e", "f"]

9. join():将一个数组的所有元素连接成一个字符串并返回这个字符串

const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join()); // output: "Fire,Air,Water"

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

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

10. toString():返回一个字符串

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

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

11. keys():返回一个包含数组中每个索引键的Array Iterator对象。

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

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

// output: 0
// output: 1
// output: 2

12. values():返回一个新的 Array Iterator 对象,该对象包含数组每个索引的值。

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

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

// output: "a"
// output: "b"
// output: "c"

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

const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0;

console.log(array.some(even)); //  output: true
// 备注: 如果用一个空数组进行测试,在任何情况下它返回的都是false。

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

const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 41];

console.log(array1.every(isBelowThreshold)); // output: true
// 备注: 若收到一个空数组,此方法在任何情况下都会返回 true。

15. includes():用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false。

const array1 = [1, 2, 3];
console.log(array1.includes(2)); // output: true

const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat')); // output: true
console.log(pets.includes('at')); // output: false

16. indexOf():返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1

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

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

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

console.log(beasts.indexOf('giraffe')); // output: -1
// 语法:arr.indexOf(searchElement[, fromIndex])

17. find():回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。

const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);

console.log(found); // output: 12

18. findIndex():返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。

const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;

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

19. findLastIndex():返回数组中满足提供的测试函数条件的最后一个元素的索引。若没有找到对应元素,则返回 -1。

const array1 = [5, 12, 50, 130, 44];
const isLargeNumber = (element) => element > 45;

console.log(array1.findLastIndex(isLargeNumber)); // output: 3

20. findLast():返回数组中满足提供的测试函数条件的最后一个元素的值。如果没有找到对应元素,则返回 undefined。

const array1 = [5, 12, 50, 130, 44];
const found = array1.findLast((element) => element > 45);

console.log(found); // output: 130

21. reduce():对数组中的每个元素按序执行一个由您提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。

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

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sum = array1.reduce(
  (previousValue, currentValue) => previousValue + currentValue,
  initialValue
);

console.log(sum); // output: 10

22. filter():创建一个新数组,其包含通过所提供函数实现的测试的所有元素

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);

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

23. forEach():对数组的每个元素执行一次给定的函数。

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

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

// output: "a"
// output: "b"
// output: "c"

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

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

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

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

console.log(array1.fill(6)); // output: [6, 6, 6, 6]
语法:
fill(value)
fill(value, start)
fill(value, start, end)

25. map():创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。

const array = [1, 4, 9, 16];
const map = array.map(x => x * 2);

console.log(map); // output: Array [2, 8, 18, 32]

相关文章

  • js基础了解

    js数组常用遍历方法使用: js数组常用操作方法使用: 基本逻辑运算: 基本字符串操作方法:

  • 这应该是JavaScript 中数组操作方法(含ES5及ES5+

    一、ES5 中数组操作方法 详尽使用参看 MDN_Array 二、ES5+ 中数组操作方法 除了 flat 和 f...

  • 前端的基本操作

    数组以及操作方法 在JavaScript中数组就是类似与Python中的列表 其实数组也是一种object 数组的...

  • 数组和对象的操作

    数组操作方法 数组中splice的操作(改变的是原数组,返回结果是分割的数组) 数组中slice的操作(不改变原数...

  • javascript高级

    数组及操作方法 数组就是一组数据的集合,javascript中,数组里面的数据可以是不同类型的 多维数组指的是数组...

  • JS学习笔记_数组篇

    (一)操作方法 push: 从数组末尾添加项,返回数组长度unshift : 从数组头添加项,返回数组长度pop...

  • 数组常用方法总结

    数组的一些操作方法?这里按照是否改变原始数组进行分类如下 1. 改变原始数组的 ``` - fill(value,...

  • 2018-07-12

    数组及操作方法 数组就是一组数据的集合,javascript中,数组里面的数据可以是不同类型的。 定义数组的方法 ...

  • JS----数组

    数组及操作方法:数组就是一组数据的集合,javascript中,数组里面的数据可以是不同类型的。 定义数组的方法:...

  • JS数组的操作方法

    数组的长度 数组的操作方法 插入和删除 从数组尾部开始 例子 从数组头部开始 例子 任意位置插入,修改和删除 sp...

网友评论

      本文标题:数组的操作方法

      本文链接:https://www.haomeiwen.com/subject/lhcknrtx.html