indexof,index,inArrayindex函数有什么用区别啊,求大神讲解

迎接黎明的曙光前行!
Array对象的方法实现(6)----Array.prototype.indexOf(实现常规参数的功能)
11,Array的indexOf方法
indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。
语法:arr.indexOf(searchElement[, fromIndex = 0])
注意:1,返回找到的索引或者不存在的-1。2,不改变原数组
Array.prototype._indexOf = function(){
if(this === null){throw new TypeError('"this" is null or not defined');}
let that = Object(this),len = that.length &&& 0,param =
if(param[1] && Math.abs(param[1])&= len)return -1;
startIndex = Math.max((param[1] ? param[1] : 0), 0) ;
while(startIndex & len){
if(startIndex in that && param[0] === that[startIndex])return startI
startIndex++;
return -1;
测试1:只有一个参数
let a = [2, 9, 7, 8, 9];
console.log(a._indexOf(2)); // 0
console.log(a._indexOf(6)); // -1
console.log(a._indexOf(7)); // 2
console.log(a._indexOf(8)); // 3
console.log(a._indexOf(9)); // 1
测试2:两个参数
let array = [2, 5, 9];
console.log(array._indexOf(2, -1)); // -1
console.log(array._indexOf(2, -3));// 0
测试3:找出指定元素出现的所有位置
var indices = [];
var array = ['a', 'b', 'a', 'c', 'a', 'd'];
var element = 'a';
var idx = array._indexOf(element);
while (idx != -1) {
indices.push(idx);
idx = array._indexOf(element, idx + 1);
console.log(indices);
// [0, 2, 4]
[我的博客,欢迎交流!](http://rattenking.gitee.io/stone/index.html)
[我的CSDN博客,欢迎交流!](https://blog.csdn.net/m0_)
[微信小程序专栏](https://blog.csdn.net/column/details/18335.html)
[前端笔记专栏](https://blog.csdn.net/column/details/18321.html)
[微信小程序实现部分高德地图功能的DEMO下载](http://download.csdn.net/download/m0_44082)
[微信小程序实现MUI的部分效果的DEMO下载](http://download.csdn.net/download/m0_96944)
[微信小程序实现MUI的GIT项目地址](https://github.com/Rattenking/WXTUI-DEMO)
[微信小程序实例列表](http://blog.csdn.net/m0_/article/details/)
[前端笔记列表](http://blog.csdn.net/m0_/article/details/)
[游戏列表](http://blog.csdn.net/m0_/article/details/)
Array.prototype.indexOf 获取元素在数组中的index
扩展Array.prototype.indexOf引发的问题
5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
关于检查对象数组的值与属性判断
JavaScript Array对象indexOf()方法的实现
js中判断数组中是否包含某元素的方法
没有更多推荐了,简单谈谈JS数组中的indexOf方法
前言相信说到 indexOf 大家并不陌生,判断字符串是否包涵子字符串时特别常用,正则不熟练同学的利器。这篇文章就最近遇到的一个问题,用实例再说说说indexOf方法。本文是小知识点积累,不作为深入讨论的话题,因此这里没有解释indexOf()的第二个参数,相信大家都知道第二个参数的作用。String 类型的使用温习一下大家熟知的字符串用法,举个12345let str = 'orange'; str.indexOf('o'); str.indexOf('n'); str.indexOf('c'); 这里 0 和 3 分别是 o 和 n 在字符串中出现的位置。起始下标是 0。而 -1 代表未匹配。曾经有人问我为什么偏偏是 -1 不是 null 或者 undefined。你去问制定规则的人啊!一脸无奈。大家看到这里感觉没什么亮点啊,别急接着再来一个例子1234let numStr = '2016'; numStr.indexOf('2'); numStr.indexOf(2); 看到这里有个小点就是 indexOf 会做简单的类型转换,把数字转换成字符串 '2' 然后再执行。Number 类型的使用大家可能会想 number 类型有没有 indexOf 方法因为会做隐式转换嘛!明确告诉大家没有,上例子123let num = 2016; num.indexOf(2); 非要对 number 类型使用 indexOf 方法嘞?那就转换成字符串咯,接着上例来写123456789num = '2016';num.indexOf(2);
num.toString().indexOf(2);
('' + num).indexOf(2); 第一种写法简单直接,对于已知的较短的数字也不是不可行。但是 num 变量针对不同数据是变化的时候,怎么办呢??第二种写法最为常用,但对比第三种写法长了一点。哈哈,其实都可以,代码洁癖的人喜欢第三种 ?Array 类型的使用大家提起精神,大boss来了。数组方法大家再熟悉不过了,却忽略了数组有 indexOf 这个方法(我个人感觉)。干说不练瞎扯淡,遇到了什么问题,注意?点又在哪里?1234567let arr = ['orange', '2016', '2016']; arr.indexOf('orange'); arr.indexOf('o');
arr.indexOf('2016'); arr.indexOf(2016); 这里没把例子拆的那么细,四个用例足以说明问题。
arr.indexOf(‘orange') 输出 0 因为 ‘orange' 是数组的第 0 个元素,匹配到并返回下标。
arr.indexOf(‘o') 输出 -1 因为此方法不会在每一个元素的基础上再次执行 indexOf 匹配。
arr.indexOf(‘2016') 输出 1 因为此方法从头匹配直到匹配到时返回第一个数组元素的下表,而不是返回全部匹配的下标。
arr.indexOf(2016) 输出 -1 注意:这里不会做隐式类型转换。既然坑已经发现我们不妨刨根问底。去MDN官网一看究竟。对此话题感兴趣的朋友可以直接跳转到只想了解的朋友下面给大家官方的 Description。
indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).一目了然,这里用的是严格等于(===)。大家做类似判断的时候多留意。不要误认为数字会转成字符串,同理字符串也不会转换成数字。总结以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
TypeError: e.indexOf is not a function解决方法
原生JavaScript实现jsindexOf()函数功能
没有更多推荐了,array.indexOf in TypeScript/JavaScript - Stack Overflow
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our , , and our .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
This question already has an answer here:
27 answers
EDIT: Although this question is marked as duplicated with . But @ssube's way is neat and much smarter.
I am using Typescript.
This works well.
var array1 = [];
array1.push(5);
array1.push(6);
console.log("a", array2.indexOf(6));
But this does not work well. Because array2.indexOf returns -1 which means it does not find it.
var array2 = [];
array2.push({aa:5,bb:5});
array2.push({aa:6,bb:6});
console.log(array2.indexOf({aa:6,bb:6}));
Looks like indexOf does not support Object. Does TypeScript have its own ways to deal with this kind of problem? Thanks.
8,6752663129
marked as duplicate by user1106925
This question has been asked before and already has an answer. If those answers do not fully address your question, please .
No. The problem is not with Object, but that you are creating two different objects.
The object literal syntax ({foo: 'bar'}) declares an object inline. When the script is executed, the object is created. Using that syntax multiple times creates multiple objects.
You can easily test that with {foo: 3} === {foo: 3}. This will evaluate to false, but they are not the same object (reference).
The indexOf method checks if the object, string, number, etc, is present in the array. You're passing a new object, which is not in the array.
If you have a reference to the object, you can use that and indexOf will work:
var foo = {aa:5,bb:5}, bar = {aa:6,bb:6};
var array2 = [];
array2.push(foo);
array2.push(bar);
console.log(array2.indexOf(foo));
Because you're referring to the same instance, this will print the index.
You can also use
with a predicate to perform a deep search:
function deepIndexOf(arr, obj) {
return arr.findIndex(function (cur) {
return Object.keys(obj).every(function (key) {
return obj[key] === cur[key];
var array2 = [];
array2.push(foo);
array2.push(bar);
console.log(deepIndexOf(array2, foo));
This won't recurse into nested objects, but will accomplish the comparison you're looking for (equivalence on two objects and their immediate fields).
28.6k571112
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled}

我要回帖

更多关于 用index嵌套match公式 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信