js Array.prototype.indexOf 获取元素在数组中的index
来源:互联网 发布:阿里云搭建个人博客 编辑:程序博客网 时间:2024/06/05 00:27
参看文档 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
用法:
从数组中获取查询元素的位置, 如果不存在则返回-1, 如果有多个重复元素,那么总是返回第一个元素
arr.indexOf(searchElemenmt [,fromIndex])
备注:
fromIndex 代表查询开始的索引, 可以是负值,eg: -2 从倒数第二个元素开始查找
eg:
查询数组中某个元素的所有的索引
var array = ['a', 'b', 'a', 'c', 'a', 'd', '1',1,'1'];function searchIndex(arr, element_search) { var index = arr.indexOf(element_search); console.log(index); if (index === -1) { return []; } var index_list = []; while (index !== -1) { index_list.push(index); index = arr.indexOf(element_search, index+1); } return index_list;}var result = searchIndex(array, '1');console.log(result);
阅读全文
0 0
- js Array.prototype.indexOf 获取元素在数组中的index
- js删除Array数组中的某个元素
- JS获取某个元素在数组中的位置
- JS数组中的indexOf方法
- JS数组中的indexOf方法
- 原声JS中indexOf()方法在数组中的应用一
- 在JavaScript的数组中进行数组元素查找和替换(JS的indexOf等)
- js 获取一个数组中的部分元素
- 简单谈谈JS数组中的indexOf方法
- 简单谈谈JS数组中的indexOf方法
- 获取元素在数组中的位置
- indexOf和lastIndexOf在Js中的应用
- 扩展Array.prototype.indexOf引发的问题
- js中数组原型Array、自定义原型函数Array.prototype
- Array.prototype.reduce统计数组中各元素出现次数
- js中数组indexOf
- js中的数组Array用法
- js数组Array元素排序sort
- Python---异常处理try...except、raise
- iOS Android微信及浏览器中唤起本地APP
- A cycle was detected in the build path of project 'core'. The cycle consists of projects {core, sms}
- Codeforces Round #436 (Div. 2) D. Make a Permutation!
- 一种基于RSI和K线的择时策略
- js Array.prototype.indexOf 获取元素在数组中的index
- Android View 四个构造函数作用
- 某安全浏览器竟然也被查出高危漏洞?开源安全问题不容忽视
- C# TimeSpan 的 Days、TotalDays、Hours、TotalHours
- JSP中forward和redirect有什么区别? 什么时候必须用哪个?
- webstorm导入背景、字体等的配置文件
- Jmeter 让CSV Data Set Config能使用Jmeter已经有的变量数据
- 元组
- Linux--日志分析查看——grep,sed,sort,awk运用