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. Wcf资料收集

    1.简介 http://www.tuicool.com/articles/mqYB32 使用规范 http://blog.51cto.com/zt/219 2.教程系列 http://www.cnbl ...

  2. intent.setFlags方法中参数值的含义

    intent.setFlags()方法中参数的含义 1.FLAG_ACTIVITY_NEW_TASK: 例如现在栈一的情况是:A    B   C(C位于栈顶),C通过intent跳转到D,并且这个I ...

  3. 浏览器d判断

    1.判断浏览器类型 if navigator.userAgent.indexOf(”MSIE”)>0) {} //判断是否IE浏览器 if(isFirefox=navigator.userAge ...

  4. html5 js跨域

    介绍 当我们使用XMLHttpRequest发送请求时,浏览器发现该请求不符合同源策略,会给该请求加一个请求头:Origin,后台进行一系列处理,如果确定接受请求则在返回结果中加入一个响应头:Acce ...

  5. Jquery 学习插件第一天

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  6. Ubuntu下搭建本地WordPress站点

    想在本地搭建WordPress博客站点作测试用?本教程一步一步教您在Linux上搭建一个LAMP(Linux, Apache, MySQL, PHP)服务器并部署WordPress博客. 请注意在复制 ...

  7. Windows下更改MySQL数据库的存储位置

    在MySQL安装完成后,要修改数据库存储的位置,比如从安装目录下的C:\Program Files\MySQL\MySQL Server 5.0\Data文件夹转移到D:\mySQLData文件夹. ...

  8. MPMoviePlayerController 电影播放器—备用

    MPMoviePlayerController 与AVAudioPlayer有点类似,前者播放视频,后者播放音频,不过也有很大不同,MPMoviePlayerController 可以直接通过远程UR ...

  9. 安卓4.2原生rom状态栏显示运营商

    前言:要调整状态栏布局,需反编译systemui.apk.单卡机修改status_bar.xml和signal_cluster_view.xml,双卡机修改gemini_status_bar.xml和 ...

  10. eclipse中JSP开发环境的配置

    1. Java环境 自行百度配置   2. Web Server环境安装: Web Server选择流行的Apache Tomcat .到http://tomcat.apache.org/  处下载, ...