js数组方法有哪些(js数组操作方法)

js数组方法有哪些(js数组操作方法)nbsp 数组有哪些基本操作 数组可以理解成是 Excel 中的列 基本上在列方向上的操作 都可以联想到 JS 数组的操作 以下是一些常见的操作 nbsp 一 添加素 push 在数组末尾添加一个或多个素 并返回新的数组长度 javascript function test add item const arr 1 2 3 arr push 4 console log JSON stringify arr nbsp



----------

▉  数组有哪些基本操作?

数组可以理解成是Excel中的列,基本上在列方向上的操作,都可以联想到JS数组的操作。

以下是一些常见的操作:

▶  一、添加素

`push()`:在数组末尾添加一个或多个素,并返回新的数组长度。

 function test_add_item(){ const arr = [1, 2, 3]; arr.push(4); console.log(JSON.stringify(arr));  } 

`unshift()`:在数组开头添加一个或多个素,并返回新的数组长度。

 function test_unshift(){ const arr = [1, 2, 3]; arr.unshift(0); console.log(JSON.stringify(arr)); // [0, 1, 2, 3] } 

▶  二、删除素

`pop()`:删除数组的最后一个素,并返回被删除的素。

 function test_pop(){ const arr = [1, 2, 3]; const removedElement = arr.pop(); console.log(JSON.stringify(arr)); // [1, 2] console.log(removedElement); // 3 } 

`shift()`:删除数组的第一个素,并返回被删除的素。

 function test_shift(){ const arr = [1, 2, 3]; const removedElement = arr.shift(); console.log(JSON.stringify(arr)); // [2, 3] console.log(removedElement); // 1 } 

▶  三、查找素

`indexOf()`:返回指定素在数组中的第一个位置,如果不存在则返回 -1。

 function test_indexof(){ const arr = [1, 2, 3, 2]; console.log(arr.indexOf(2)); // 1 } 

`lastIndexOf()`:返回指定素,在数组中的最后一个位置,如果不存在则返回 -1。

 function test_lastIndexOf(){ const arr = [1, 2, 3, 2]; console.log(arr.lastIndexOf(2)); // 3 } 

`includes()`:判断数组是否包含指定素,返回true或false。

 function test_includes(){ const arr = [1, 2, 3]; console.log(arr.includes(2)); // true } 

▶  四、数组遍历

前面已经讲过了,关注拉小登Excel,参考前面的教程

▶  五、数组拼接和切片

`concat()`:连接两个或多个数组,返回一个新数组。

 function test_concat(){ const arr1 = [1, 2]; const arr2 = [3, 4]; const combinedArray = arr1.concat(arr2); console.log(JSON.stringify(combinedArray)); // [1, 2, 3, 4] } 

`slice()`:从数组中提取一部分,返回一个新数组。下面的代码,把第2~第5个素提取出来,生成了一个新的数组。

 function test_slice(){ let arr = [1, 2, 3, 4, 5]; let slicedArray = arr.slice(1, 4); console.log(JSON.stringify(slicedArray)); // [2, 3, 4] } 

slice函数的用法如下:

 array.slice(start, end) 

- `start`:可选参数,表示提取开始的位置索引,默认为 0。如果是负数,则表示从数组末尾开始计数。

- `end`:可选参数,表示提取结束的位置索引(不包括该位置的素),默认为数组的长度。如果是负数,则表示从数组末尾开始计数。

▶  六、行列拼接

在Excel总还有vstack和HSTACK两个函数,分别用来讲多列、多行拼接成一个表格。

JSA中没有类似的函数,所以我就自己写了一个。

vstack函数。把多行数据按列方向,向下拼接。

 function test_vstack(){ const arr1 = [1, 2]; const arr2 = [3, 4]; const combinedArray = [arr1,arr2]; Range("A1:B2").Value2= combinedArray; console.log(JSON.stringify(combinedArray));  } 

计算结果是:[[1,2],[3,4]]

赋值到表格中,效果如下:

hstack函数。把多列数据按行方向,向右拼接。

 function test_hstack(){ const arr1 = [1, 2]; const arr2 = [3, 4]; const combinedArray = [arr1,arr2]; Range("A1:B2").Value2= transpose(combinedArray); console.log(JSON.stringify(combinedArray));  } function transpose(arr){ const transposed = []; for (let i = 0; i < arr[0].length; i++) {   transposed[i] = [];   for (let j = 0; j < arr.length; j++) {     transposed[i][j] = arr[j][i];   } } return transposed; } 

把数据赋值到表格中,效果如下:

▶  六、数组排序和反转

`sort()`:对数组进行排序,默认按照字典序排序。可以传入比较函数来自定义排序规则。

 function test_sort(){ const arr = [3, 1, 2]; arr.sort(); console.log(JSON.stringify(arr)); // [1, 2, 3] } 

`reverse()`:反转数组的顺序。

 function test_reverse(){ const arr = [1, 2, 3]; arr.reverse(); console.log(JSON.stringify(arr)); // [3, 2, 1] } 

▶  七、其他操作

`map()`:对数组中的每个素应用一个函数,返回一个新数组。

下面的代码,对数组中的每个数组乘以了2。

 function test_map(){ const arr = [1, 2, 3]; const mappedArray = arr.map(item => item * 2); console.log(JSON.stringify(mappedArray)); // [2, 4, 6] } 

`filter()`:过滤数组中的素,返回一个新数组,其中包含通过测试函数的素。

下面的代码,用来筛选出数组中所有的偶数。

 function test_filter(){ const arr = [1, 2, 3, 4]; const filteredArray = arr.filter(item => item % 2 === 0); console.log(JSON.stringify(filteredArray)); // [2, 4] } 

`reduce()`:对数组中的素进行累积操作,返回一个单一的值。用法和Excel中的reduce函数类似。

 function test_reduce(){ const arr = [1, 2, 3]; const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); console.log(sum); // 6 } 

▶  清空数组

将数组的长度length设置为0,即可以清空数组中的所有内容,得到一个空白的数组。

 function test_清空(){ let arr = [1, 2, 3]; arr.length = 0; console.log(JSON.stringify(arr)); // [] } 
今天的文章 js数组方法有哪些(js数组操作方法)分享到此就结束了,感谢您的阅读。
编程小号
上一篇 2026-01-20 20:17
下一篇 2026-01-20 21:33

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/34176.html