The correct usage of js indexOf
indexOf plays an important role in js. It can determine whether an element exists in an array, or whether a character exists in a string. If there is an index that returns the location where the element or character appears for the first time, it does not exist and returns -1.
For example
var arr = [1, 2, 3]; ((2)); //The print result is1
Or
var str = "helloworld"; (("w")); //The print result is5
Then, when you want to delete an element in an array, you often write this
var arr = [1, 2, 3]; var idx = (2); (idx,1); (arr);
But is indexOf really a good thing? Look at the code below.
var arr = [{name:"racyily",age:22},{name:"susan",age:18}]; var obj = {name:"susan",age:18}; ((obj)); //The print result is-1
We found that obj and arr array are the same as the first element. But it returns -1.
Try this again
var arr = [{name:"racyily",age:22},{name:"susan",age:18}]; var arr2 = arr[1]; ((arr2)); //The print result is1
Now I understand that it is because if the array stores an object or an array, it must be a reference to the object to obtain the correct index value using indexOf.
So, if you want to determine whether an object (array) exists in an array (value is equal to an element), how to implement it?
I can only write a method to implement it myself.
That's how I wrote it at first
var myIndexOf = function(arr,el){ for(var i=0;i<;i++){ if((arr[i]) == (el)){ return i; } } return -1; }
The principle implemented in the above code is to convert the elements in the array and the objects passed in to a string through () and then compare whether the two strings are equal.
This method seems to implement the function, but a deep pit is hidden in it.
Because once the order of fields in the object is reversed, it will not match, and -1 is returned (I was deeply hurt by this pit).
So how to avoid such a problem? In principle, it is to compare whether each element in the object is equal.
Look at the following code
// Find out whether an object (array) exists in an arrayfunction myIndexOf(arr, el) { var result = false; if (arr instanceof Array && el instanceof Object) { for (var i in arr) { if(checkLen(arr[i],el)){ result = recursiveFunc(arr[i], el); } if (result) { return i; } } return -1; } return -1; }
//Recursively call the comparison object for each fieldvar recursiveFunc = function (arr, o) { var result = false; if (o instanceof Object) { if (!(arr instanceof Object)) { return false; } for (var p in arr) { if(checkLen(arr[p],o[p])){ result = recursiveFunc(arr[p], o[p]); if (!result) { return false; } } } return true; } else { if (arr == o) { return true; } return false; } }
//Judge whether the two objects have the same length.var checkLen = function (o1, o2) { var count1 = 0, count2 = 0; if(o1 instanceof Object && o2 instanceof Object){ for(var i in o1){ count1++; } for(var p in o2){ count2++; } } return count1==count2; }
Supplement: Use of indexOf() in js
grammar:
(Char, [startIndex], [count]):
definition
---Returns the index of the first match of the specified character in the original string. If the second parameter is omitted, the search will start from the first character of the string.
-----You can specify the character to start searching for characters with the position and length. If the character is not found, return -1.
----- You can also determine whether an array contains a certain value.
Usage and examples
Example 1: Find the index of a character in a string that first appears from scratch
var str = "Hello world!" (("o")) //4 (("Hello")) //0 (("World")) //-1 (("world")) //6
It should be noted that when a string is matched, the index of the first character in the string will be returned. As in the example above, the return is 6.
Example 2: Find the index of a character in a string that first appears from the specified position
var str = "Hello world! wo shi ooo" (("o",8)) //14
There is indexOf, and there is lastIndexOf. It is the index that matches the last occurrence of the string.
var str = “Hello world! wo shi oll”
((“o”)) //4
((“o”)) //20`
Example 3: In a scenario where a character is included in a string:
var str = "Hello world! wo shi oll" (("World") == -1) //true (("world") == -1) //false
Example 4: Determine whether an element is included in the array
const arr = [1,5,3,8,22] ((2))//Not exists, return -1((8))//exist,Return to index3
This is the end of this article about the correct usage of js indexOf. For more related content on usage of js indexOf, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!