+-
数组将要新增的方法:array.at(index)
首页 专栏 javascript 文章详情
3

数组将要新增的方法:array.at(index)

疯狂的技术宅 发布于 今天 01:00

除了普通对象之外,数组是 JavaScript 中使用最广泛的数据结构。数组上最常使用的操作是按索引访问元素。

本文介绍新的数组方法 array.at(index)

新方法最主要好处是可以用负索引从数组末尾访问元素,而平时使用的方括号语法 array[index] 则没有办法做到。

方括号语法的局限性

通常按索引访问数组元素的方法是使用方括号语法 array[index]

const fruits = ['orange', 'apple', 'banana', 'grape'];

const item = fruits[1];
item; // => 'apple'

表达式 array[index] 的执行结果是位于 index 位置的数组元素项,JavaScript 中数组的索引从 0 开始,这些你肯定知道。

通常方括号语法是一种通过正索引(>= 0)访问数组元素的方法。它的语法简单易读。

但有时我们希望从末尾开始访问元素。例如:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const lastItem = fruits[fruits.length - 1];
lastItem; // => 'grape'

fruits[fruits.length-1] 是访问数组最后一个元素的方式,其中fruits.length-1 是最后一个元素的索引。

问题在于方括号不允许直接从数组末尾访问元素,也不能接受负索引。

幸运的是,一项新的提案(截至2021年1月的第3阶段)将 at() 方法引入了数组(以及类型化数组和字符串),并解决了方括号的许多限制。

array.at() 方法

简而言之,array.at(index) 用来访问处于 index 位置的元素。

如果 index 是一个正整数 >= 0,则该方法返回这个索引位置的元素:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const item = fruits.at(1);
item; // => 'apple'

如果 index 参数大于或等于数组长度,则像方括号语法一样返回 undefined

const fruits = ['orange', 'apple', 'banana', 'grape'];

const item = fruits.at(999);
item; // => undefined

当对 array.at() 方法使用负索引时,会从数组的末尾访问元素。

例如用索引 -1 来访问数组的最后一个元素:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const lastItem = fruits.at(-1);
lastItem; // => 'grape'

下面是更详细的例子:

const vegetables = ['potatoe', 'tomatoe', 'onion'];

vegetables.at(0); // => 'potatoe'
vegetables.at(1); // => 'tomatoe'
vegetables.at(2); // => 'onion'
vegetables.at(3); // => undefined

vegetables.at(-1); // => 'onion'
vegetables.at(-2); // => 'tomatoe'
vegetables.at(-3); // => 'potatoe'
vegetables.at(-4); // => undefined

如果 negIndex 是一个负索引 < 0,那么 array.at(negIndex) 将会访问位于索引 array.length + negIndex 处的元素。例如:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const negIndex = -2;

fruits.at(negIndex);              // => 'banana'
fruits[fruits.length + negIndex]; // => 'banana'

总结

JavaScript 中的方括号语法是按索引访问项目的常用方法。只需将索引表达式放在方括号 array[index] 中,然后既可以获取在该索引处的数组项。

但是有时这种方式并不方便,因为它不接受负索引。所以要访问数组的最后一个元素,需要用这种方法:

const lastItem = array[array.length - 1];

新的数组方法 array.at(index) 使你可以将索引作为常规访问器访问数组元素。此外,array.at(index)接受负索引,在这种情况下,该方法从头开始获取元素:

const lastItem = array.at(-1);

现在只需要把 array.prototype.at polyfill 包含到你的应用程序中,就可以使用 array.at() 了。

本文首发微信公众号:前端先锋

欢迎扫描二维码关注公众号,每天都给你推送新鲜的前端技术文章

欢迎继续阅读本专栏其它高赞文章:

深入理解Shadow DOM v1 一步步教你用 WebVR 实现虚拟现实游戏 13个帮你提高开发效率的现代CSS框架 快速上手BootstrapVue JavaScript引擎是如何工作的?从调用栈到Promise你需要知道的一切 WebSocket实战:在 Node 和 React 之间进行实时通信 关于 Git 的 20 个面试题 深入解析 Node.js 的 console.log Node.js 究竟是什么? 30分钟用Node.js构建一个API服务器 Javascript的对象拷贝 程序员30岁前月薪达不到30K,该何去何从 14个最好的 JavaScript 数据可视化库 8 个给前端的顶级 VS Code 扩展插件 Node.js 多线程完全指南 把HTML转成PDF的4个方案及实现 更多文章...
javascript 前端
阅读 114 发布于 今天 01:00
赞3 收藏2
分享
本作品系原创, 采用《署名-非商业性使用-禁止演绎 4.0 国际》许可协议
疯狂的技术宅
本专栏文章首发于公众号:前端先锋 。
关注专栏
avatar
疯狂的技术宅

资深技术宅,爱好广泛,兴趣多变。博览群书,喜欢扯淡。十八种语言样样稀松。想要了解更多,请关注微信公众号:充实的脑洞。

41.8k 声望
5.9k 粉丝
关注作者
0 条评论
得票 时间
提交评论
avatar
疯狂的技术宅

资深技术宅,爱好广泛,兴趣多变。博览群书,喜欢扯淡。十八种语言样样稀松。想要了解更多,请关注微信公众号:充实的脑洞。

41.8k 声望
5.9k 粉丝
关注作者
宣传栏
目录

除了普通对象之外,数组是 JavaScript 中使用最广泛的数据结构。数组上最常使用的操作是按索引访问元素。

本文介绍新的数组方法 array.at(index)

新方法最主要好处是可以用负索引从数组末尾访问元素,而平时使用的方括号语法 array[index] 则没有办法做到。

方括号语法的局限性

通常按索引访问数组元素的方法是使用方括号语法 array[index]

const fruits = ['orange', 'apple', 'banana', 'grape'];

const item = fruits[1];
item; // => 'apple'

表达式 array[index] 的执行结果是位于 index 位置的数组元素项,JavaScript 中数组的索引从 0 开始,这些你肯定知道。

通常方括号语法是一种通过正索引(>= 0)访问数组元素的方法。它的语法简单易读。

但有时我们希望从末尾开始访问元素。例如:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const lastItem = fruits[fruits.length - 1];
lastItem; // => 'grape'

fruits[fruits.length-1] 是访问数组最后一个元素的方式,其中fruits.length-1 是最后一个元素的索引。

问题在于方括号不允许直接从数组末尾访问元素,也不能接受负索引。

幸运的是,一项新的提案(截至2021年1月的第3阶段)将 at() 方法引入了数组(以及类型化数组和字符串),并解决了方括号的许多限制。

array.at() 方法

简而言之,array.at(index) 用来访问处于 index 位置的元素。

如果 index 是一个正整数 >= 0,则该方法返回这个索引位置的元素:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const item = fruits.at(1);
item; // => 'apple'

如果 index 参数大于或等于数组长度,则像方括号语法一样返回 undefined

const fruits = ['orange', 'apple', 'banana', 'grape'];

const item = fruits.at(999);
item; // => undefined

当对 array.at() 方法使用负索引时,会从数组的末尾访问元素。

例如用索引 -1 来访问数组的最后一个元素:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const lastItem = fruits.at(-1);
lastItem; // => 'grape'

下面是更详细的例子:

const vegetables = ['potatoe', 'tomatoe', 'onion'];

vegetables.at(0); // => 'potatoe'
vegetables.at(1); // => 'tomatoe'
vegetables.at(2); // => 'onion'
vegetables.at(3); // => undefined

vegetables.at(-1); // => 'onion'
vegetables.at(-2); // => 'tomatoe'
vegetables.at(-3); // => 'potatoe'
vegetables.at(-4); // => undefined

如果 negIndex 是一个负索引 < 0,那么 array.at(negIndex) 将会访问位于索引 array.length + negIndex 处的元素。例如:

const fruits = ['orange', 'apple', 'banana', 'grape'];

const negIndex = -2;

fruits.at(negIndex);              // => 'banana'
fruits[fruits.length + negIndex]; // => 'banana'

总结

JavaScript 中的方括号语法是按索引访问项目的常用方法。只需将索引表达式放在方括号 array[index] 中,然后既可以获取在该索引处的数组项。

但是有时这种方式并不方便,因为它不接受负索引。所以要访问数组的最后一个元素,需要用这种方法:

const lastItem = array[array.length - 1];

新的数组方法 array.at(index) 使你可以将索引作为常规访问器访问数组元素。此外,array.at(index)接受负索引,在这种情况下,该方法从头开始获取元素:

const lastItem = array.at(-1);

现在只需要把 array.prototype.at polyfill 包含到你的应用程序中,就可以使用 array.at() 了。

本文首发微信公众号:前端先锋

欢迎扫描二维码关注公众号,每天都给你推送新鲜的前端技术文章

欢迎继续阅读本专栏其它高赞文章:

深入理解Shadow DOM v1 一步步教你用 WebVR 实现虚拟现实游戏 13个帮你提高开发效率的现代CSS框架 快速上手BootstrapVue JavaScript引擎是如何工作的?从调用栈到Promise你需要知道的一切 WebSocket实战:在 Node 和 React 之间进行实时通信 关于 Git 的 20 个面试题 深入解析 Node.js 的 console.log Node.js 究竟是什么? 30分钟用Node.js构建一个API服务器 Javascript的对象拷贝 程序员30岁前月薪达不到30K,该何去何从 14个最好的 JavaScript 数据可视化库 8 个给前端的顶级 VS Code 扩展插件 Node.js 多线程完全指南 把HTML转成PDF的4个方案及实现 更多文章...