[Javascript] Array methods in depth - indexOf
2015-12-30 22:23
756 查看
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.
indexOf can take second params for telling the startLookingIndex:
Example:
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);
相关文章推荐
- JSP中两种include的区别
- c# Json Dictionary序列化和反序列化
- Weblogic内存回收机制设计不合理导致服务器CPU使用率100%
- jsp知识点
- JavaScript中JSON的处理心得
- JSON与XML的区别比较
- js中的"=="和equals()以及is()三者的区别
- HTML+CSS+javaScript 基础知识(三)
- jsp生成二维码
- JSON解析(1)
- js指定多个分割字符分割到同个数组中
- 124 js 前台日期显示格式和操作 & js生成table
- JavaScript全讲-必知的特性
- Javascript面向对象及组件详细介绍(五)原型链
- Javascript面向对象及组件详细介绍(四)包装对象
- Javascript面向对象及组件的详细介绍(三)面向对象的拖拽
- JavaScript字符串转换日期
- Javascript面向对象及组件开发(二)面向对象的写法
- Javascript中的几种继承方式比较
- Javascript中的几种继承方式比较