indexOf is used to search for a value or reference inside of an array. In this lesson we first look at what values are returned when a search is successful vs when it's unsuccessful. Then we move onto a technique that shows how to use the return value to create a boolean flag that can be checked easily. We end by filtering 1 array based on the existence of a value in a whitelist array.

var people = ['Wan', 'John', 'Kate', 'Joe'];
var indexJoe = people.indexOf('Joe');
var findJoe = people.indexOf('Joe') > -1; console.log(indexJoe, findJoe); //3, true

indexOf can take second params for telling the startLookingIndex:

var people = ['Wan','Joe', 'John', 'Kate'];
var indexJoe = people.indexOf('Joe');
var findJoe = people.indexOf('Joe', 2) > -1; console.log(indexJoe, findJoe); //1, false var findJoe = people.indexOf('Joe', 1) > -1;
console.log(indexJoe, findJoe); //1, true

Example:

var whitelist = ['.css', '.js'];

var events = [
{
file: 'css/core.css'
},
{
file: 'js/app.js'
},
{
file: 'index.html'
}
]; var filtered = events.filter(event => {
var ext = event.file.substr(event.file.lastIndexOf('.'));
return whitelist.indexOf(ext) > -1;
}); console.log(filtered);

[Javascript] Array methods in depth - indexOf的更多相关文章

  1. [Javascript ] Array methods in depth - sort

    Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...

  2. [Javascript] Array methods in depth - filter

    Array filter creates a new array with all elements that pass the test implemented by the provided fu ...

  3. [Javascript] Array methods in depth - slice

    Array slice creates a shallow copy of an array. In this lesson we cover, in detail, exactly what a ' ...

  4. [Javascript] JavaScript Array Methods in Depth - push

    Array push is used to add elements to the end of an Array. In this lesson we'll see how the push met ...

  5. [Javascript] Array methods in depth - some

    some returns a boolean value after passing each item in the source array through the test function t ...

  6. JavaScript Array methods performance compare

    JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...

  7. javascript Array Methods(学习笔记)

    ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach();  2.map();  3.filter();  4.every();  5.some();  6.reduce() ...

  8. JavaScript Array 对象

    JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...

  9. 修复浏览器不支持Array自带的indexOf方法的扩展

    JavaScript中Array的indexOf方法支持的浏览器有:IE9+.Firefox 2+.Safari 3+.Opera 9.5+和Chrome 如果想要在不支持的浏览器中使用indexOf ...

随机推荐

  1. CI框架深入篇(2)一些基础的我之不知道的标准格式

    1,一些命名规则:类文件名必大写,其他配置文件,视图文件或着脚本都要小写,类文件名和类名要一致!! 2,类名要大写开头,若是多个单词,那就下划线不要驼封法: 3,变量名要小写全,多个单词下划线分割,后 ...

  2. win7下如何执行批处理文件

    经过了一段时间的适应之后,某C也基本摸透了Win7的习性,然后突然发现无聊,就上了VeryCD去下载几个游戏玩.R大是电驴游戏版块的大神,某C怀着崇敬的心情,每每都追寻着他的足迹下载游戏.这次正好下载 ...

  3. MYSQL注释

    MYSQL扩展了SQL的注释/**/, /*! (语句)#加感叹号,内部语句会被执行 */ /*!50001 select * from test #表示数据库为5.00.01版本,内部语句会被执行 ...

  4. oracle 存储过程,函数和包

    创建存储过程: 语法:create [or replace] PROCEDURE 过程名(参数列表)  AS PLSQL子程序体: 调用 存储过程的方式 两种1.execute(exec)     - ...

  5. C# 缩略图算法

    代码写多了,有些使用过的方法和技巧会一时半会想不起来,平日记录下来,方便自己和有需要的人日后查阅. using (var stream = new FileStream(physicalPath, F ...

  6. Redis同步(主从复制)

    目录1.Replication的工作原理2.如何配置Redis主从复制3.应用示例 1.Replication的工作原理在Slave启动并连接到Master之后,它将主动发送一条SYNC命令.此后Ma ...

  7. Phpcms V9全站伪静态设置方法

    为什么要伪静态?具体在这里就不说了,你懂的!一方面更新修改后不需要生成静态文件,另一方面为了SEO! 访问规则如下 1 2 list-{$catid}-{$page}.html content-{$c ...

  8. table 自动换行

    <table border=" align="center" style="table-layout:fixed;word-wrap:break-word ...

  9. linux2.6.32 内核源码树解析与整理

    一 系统最核心组件目录: 1 arch目录该目录中的每个子目录中都与某种体系结构相对应,用于存放体系结构相关代码,向平台无关的系统核心模块提供所需的功能接口.每个体系结构对应的子目录下通常至少包含以下 ...

  10. 开发H5小游戏

    Egret白鹭H5小游戏开发入门(一)   前言: 好久没更新博客了,以前很多都不会,所以常常写博客总结,倒是现在有点点经验了就懒了.在过去的几个月里,在canvas游戏框架方面,撸过了CreateJ ...