JavaScript 数组

2024-08-05 0 441

JavaScript 数组

什么是数组?

数组是一种存储有序元素集合的数据结构。在 JavascrIPt 中,数组被归类为一种特殊类型的对象,可以存储数字、字符串、对象或其他数组。数组中的元素括在方括号 [ ] 中并使用从零开始的索引。从零开始的索引意味着数组的第一个元素的索引为 0,第二个元素的索引为 1,依此类推。

1

2

3

4

5

6

7

const names = ["david", "hannah", "william"];

console.log(names[0]); // returns the first element

// returns "david"

console.log(names[1]); // returns the second element

// returns "hannah"

console.log(names[2]); // returns the third element

// returns "william"

如何修改或操作数组?

数组中元素的索引

可以通过为空索引赋值来将新元素添加到数组中。

1

2

3

names[3] = "eric";

console.log(names);

// returns ["david", "hannah", "william", "eric"]

可以通过为现有索引重新分配新值来修改数组中的元素。

1

2

3

names[1] = "juniper";

console.log(names);

// returns ["david", "juniper", "william", "eric"]

数组方法

数组还可以使用数组方法进行修改或操作,例如 ‘push’、’pop’、’unshift’、’shift’、’slice’ 和 ‘splice’。

‘推()’

‘push’ 方法接受一个或多个元素作为参数,将元素添加到数组末尾,并返回修改后的数组的长度。

1

2

3

4

names.push("bob");

// returns 5

console.log(names);

// returns ["david", "juniper", "william", "eric", "bob"]

‘流行音乐()’

‘pop’ 方法不带参数,删除数组的最后一个元素,并返回删除的元素。

1

2

3

4

names.pop();

// returns "bob"

console.log(names);

// returns ["david", "juniper", "william", "eric"]

‘取消移位()’

‘unshift’ 方法接受一个或多个元素作为参数,将元素添加到数组的开头,并返回修改后的数组的长度。

1

2

3

4

names.unshift("jack", "jane");

// returns 6

console.log(names);

// returns ["jack", "jane", "david", "juniper", "william", "eric"]

‘转移()’

“shift”方法不带参数,删除数组的第一个元素,并返回删除的元素。

1

2

3

4

names.shift();

// returns "jack"

console.log(names);

// returns ["jane", "david", "juniper", "william", "eric"]

‘片()’

“slice”方法采用两个可选参数(startindex、endindex),并返回一个新数组,其中包含从 startindex 到(但不包括)原始数组的 endindex 的元素。
如果省略 startindex,则使用 0。
如果省略 endindex,则使用数组长度。负索引号可用于从数组末尾开始倒数。

1

2

3

4

5

6

7

8

names.slice(1, 3);

// returns ["david", "juniper"]

names.slice(3);

// returns ["juniper", "william", "eric"]

names.slice(-2, 1);

// returns ["william", "eric", "jane"]

names.slice();

// returns ["jane", "david", "juniper", "william", "eric"]

‘拼接()’

“splice”方法接受一个或多个参数(startindex、deletecount、element1、element2…)并返回一个包含所有删除元素的新数组。从startindex 开始,删除deletecount 个元素,并将以下元素参数添加到从startindex 开始的数组中。如果省略deletecount,则删除从startindex 到数组末尾的所有元素。如果省略元素参数,则不会添加任何元素。

1

2

3

4

5

6

7

8

9

10

11

12

names.splice(0, 1, "joe", "alex");

// returns ["jane"]

console.log(names);

// returns ["joe", "alex", "david", "juniper", "william", "eric"]

names.splice(1, 4);

// returns ["alex", "david", "juniper", "william"]

console.log(names);

// returns ["joe", "eric"]

names.splice(0, 0, "bob", "frank", "maria")

// returns []

console.log(names);

// returns ["joe", "bob", "frank", "maria", "eric"]

由于“push”、“pop”、“unshift”、“shift”和“splice”修改了原始数组,因此它们被归类为破坏性方法。 “切片”方法使原始数组保持完整,因此它被归类为非破坏性。

扩展运算符 ‘…’

要无损地向数组添加元素或复制数组,可以使用扩展运算符。扩展运算符将数组扩展为其元素。

1

2

3

4

5

const array = [1, 2, 3];

const newarray = [0, ...array, 4, 5];

// ...array spreads [1, 2, 3] into 1, 2, 3

console.log(newarray);

// returns [1, 2, 3, 4, 5]

如果没有扩展运算符,原始数组将嵌套在新数组中。

1

2

3

4

const array = [1, 2, 3];

const newarray = [0, array, 4, 5];

console.log(newarray);

// returns [0, [1, 2, 3], 4, 5];

迭代数组方法

迭代数组方法对数组中的每个元素调用提供的函数并返回一个值或新数组。使用三个参数调用提供的函数:当前元素、当前元素的索引以及调用该方法的原始数组。

1

2

3

function callbackfunction (currentelement, currentindex, originalarray) {

// function body

}

迭代数组方法的一些示例是:“find”、“filter”、“map”和“reduce”。

‘寻找()’

‘find’方法接受一个函数作为参数,并返回数组中满足函数条件的第一个元素。

1

2

3

const numbers = [5, 10, 15, 20, 25];

numbers.find(number => number > 15);

// returns 20;

‘筛选()’

“filter”方法与“find”方法类似,但返回满足给定函数条件的所有元素的数组。

1

2

3

const numbers = [5, 10, 15, 20, 25];

numbers.filter(number => number > 15);

// returns [20, 25];

地图()’

“map”方法返回一个新数组,其中包含对原始数组中每个元素调用该函数的结果。

1

2

3

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

numbers.map(number => number * number);

// returns [1, 4, 9, 16, 25]

‘减少()’

“reduce”方法接受一个函数和一个初始值作为参数。提供的函数接收四个参数:累加器、当前值、当前索引和原始数组。提供的初始值是数组第一个元素的累加器的值。每个元素的函数结果用作数组中下一个元素的累加器的值。如果未提供初始值,则将累加器设置为数组的第一个元素,并从第二个元素开始调用回调函数。

1

2

3

const numbers = [1, 2, 3, 4, 5]

numbers.reduce(((acc, number) => acc + number), 0);

// returns 15

登录后复制

 

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 JavaScript JavaScript 数组 https://www.xuezuoweb.com/8964.html

常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用