[Javascript] Array methods in depth - indexOf
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的更多相关文章
- [Javascript ] Array methods in depth - sort
Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...
- [Javascript] Array methods in depth - filter
Array filter creates a new array with all elements that pass the test implemented by the provided fu ...
- [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 ' ...
- [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 ...
- [Javascript] Array methods in depth - some
some returns a boolean value after passing each item in the source array through the test function t ...
- JavaScript Array methods performance compare
JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...
- javascript Array Methods(学习笔记)
ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach(); 2.map(); 3.filter(); 4.every(); 5.some(); 6.reduce() ...
- JavaScript Array 对象
JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...
- 修复浏览器不支持Array自带的indexOf方法的扩展
JavaScript中Array的indexOf方法支持的浏览器有:IE9+.Firefox 2+.Safari 3+.Opera 9.5+和Chrome 如果想要在不支持的浏览器中使用indexOf ...
随机推荐
- Asp.Net Mvc MapRoute .html不起作用(转)
RegisterRoutes 注册路由示例配置: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRo ...
- SQL从入门到基础 - 01 数据库开发及ADO.Net
一.数据库概述 1. 用自定义文件格式保存数据的劣势:并发性差,查找数据的速度差. 2. DBMS(DataBase Management System数据库管理系统)和数据库.平时谈到“数据库”的含 ...
- java socket报文通信(一)socket的建立
java socket报文通信(一) socket的建立 今天来和大家分享一下java中如何使用socket进行通信.先来啰嗦两句,看看Tcp/ip和udp: TCP是Transfer Contro ...
- PHP String
PHP 5 String 函数 PHP String 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. 函数 描述 addcslashes() 返回在指定的字符前添加反斜杠的字符串. add ...
- String的format方法
摘自:http://xiongzhenhui.iteye.com/blog/940416 http://blueram.iteye.com/blog/441683 一.常规类型.字符类型和数值类型的格 ...
- display:inline,display:inline-block,display:block 区别
之前一直迷惑于display:inline/inline-block/block的异同,在度娘谷哥的帮助下,突然有了一点思路. 按照网上的介绍,inline将对象转化为内联元素,block将对象转化为 ...
- MySQL重构查询的方式
在优化有问题的查询时,目标应该是找到一个更优的方法获得实际需要的结果--而不一定总要从MySQL获取一模一样的结果集.有时候可以查询转换一种写法让其返回一样的结果,但是性能更好.但也可以通过修改应用代 ...
- C# .NET 使用第三方类库DotNetZip解压/压缩Zip rar文件
DotNetZip on CodePlex: http://dotnetzip.codeplex.com/ 详细的可以看源代码……总之感觉比SharpZipLib好用.而且DotNetZip支持VB, ...
- Jquery插件模版
;(function($){ $.fn.jcDate = function(options) { var defaults = { IcoClass : "jcDateIco", ...
- Android Framework------之Property子系统
概述 Property是Android系统中一个重要的概念,在Android系统内,主要用于系统配置,以及不同服务间的简单信息分享.比如设备名字,蓝牙名字,编译信息,网络dns地址,以及其他的一些基本 ...