未加星标

Search JavaScript Arrays Efficiently with includes and indexOf

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Searching througharrays has traditionally involved writing an exhaustiveloop that inspected each element in the array, checking if they matched a particular value. Thankfully, modernjavascript provides two solutions that are far more efficient: one new in JS 2015, and one a little older.

includes

We’ll start with the most recent: includes . Very simply, includes returns a value of true or false if an array contains a particular value. Given an array of the following elements:

let stations = [ "Salyut", "Skylab", "Almaz", "Mir", "ISS", "Tiangong" ]

Determining if the array contains a particular value is as simple as the following (tested in theconsole for clarity):

stations.includes("Skylab"); > true stations.includes("Space Station V"); > false

Note that includes will only match complete elements, and not partial strings:

stations.includes("Sky"); > false

Searches are also case-sensitive:

stations.includes("skylab"); > false

includes will tell you if an array contains a matching element, but it won’t tell you where that element is in the array. However, you can tell it where to start searching by adding an optional position argument:

stations.includes("Mir", 2); > true stations.includes("Mir", 5); > false

includes has decent support, in recent browser versions: Chrome 47, Firefox 43. Microsoft Edge, and Safari 9. There’s no support in Internet Explorer, but polyfills.io and other sources can provide a solution.

indexOf

indexOf tells you the position of a matching element in an array. Given the same array we started with:

let stations = [ "Salyut", "Skylab", "Almaz", "Mir", "ISS", "Tiangong" ];

We can use indexOf to detect the position of an element that matches a particular value:

stations.indexOf("ISS"); > 4

indexOf returns -1 if the element is not found:

stations.indexOf("Elysium"); > -1

The method has excellent support in every modern browser, including IE9+. Like includes , you can add an optional argument for where in the array you want to start searching from.

lastIndexOf

indexOf finds the first match from the start of an array. In contrast, lastIndexOf starts searching from the end of the array.

stations.lastIndexOf("Almaz"); > 2

Note that lastIndexOf still reports the position of a match indexed from the start, and (like indexOf ) will return -1 if no match is found. Also like indexOf , an optional argument can be provided, but it indicates the point from the end of the array where the search should start from. As you might expect, lastIndexOf has the same range of browser support as indexOf .

Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJavaChromeFirefox
分页:12
转载请注明
本文标题:Search JavaScript Arrays Efficiently with includes and indexOf
本站链接:http://www.codesec.net/view/485622.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(31)