The remaining common methods for arrays are:
Sequence Number | Method | Definition | Return value |
---|---|---|---|
10 | indexOf() | Gets the index of the array item in the array | Index Value |
11 | lastIndexOf () | Gets the last index of an array item that appears in the array | Index Value |
12 | forEach() | Loop through arrays | Return undefined by default |
13 | map() | Loop through arrays | - |
10, indexOf(): Returns the position of the first occurrence of a specified string value in a string.
grammar
stringObject.indexOf(searchvalue,fromindex)
Parameter details
parameter | describe |
---|---|
searchvalue | Required.Specifies the string value to retrieve. |
fromindex | Optional integer parameter.Specifies where to start the retrieval in the string.Its legal value is 0 to stringObject.length - 1.If omitted, the first character of the string is retrieved. |
Explain
This method retrieves the string stringObject from beginning to end to see if it contains a substring search value.
The start of the retrieval is at the fromindex of the string or at the beginning of the string (when no fromindex is specified).
If a search value is found, the first occurrence of the search value is returned.
The character position in stringObject starts at 0.
Be careful
The indexOf() method is case sensitive!
If the string value to retrieve does not appear, the method returns -1.
Code
// indexOf() The position of the first occurrence of a specified string value in a string. var arr1 = ['0','1','2','3','hello','world']; var a = arr1.indexOf('hello', 1); console.log(a) // 4 var a = arr1.indexOf('hello', 5); console.log(a) // -1 (return-1 if not found)
11, lastIndexOf (): Gets the last index of an array item that appears in the array
grammar
string.lastIndexOf(searchvalue,start)
Parameter details
parameter | describe |
---|---|
searchvalue | Required.Specifies the string value to retrieve. |
Explain
The method retrieves the array from the back to the front, but returns the last occurrence of the element calculated from the starting position (0).
The lastIndexOf() method is case sensitive!
Code
// lastIndexOf() Gets the last index of an array item that appears in the array var arr1 = ['0','1','2','3','hello','world','0']; var a = arr1.lastIndexOf('0'); console.log(a) // 6
12, forEach(): Used to call each element of an array and pass the element to the callback function.
grammar
array.forEach(function(currentValue, index, arr), thisValue)
Parameter details
parameter | describe |
---|---|
function(currentValue, index, arr) | Required.The function to be called for each element in the array. |
currentValue (parameter in function) | Required.Current element. |
index (parameter within function) | Optional.The index value of the current element. |
arr (parameter in function) | Optional.Array object to which the current element belongs. |
thisValue | Optional.The value passed to the function is typically "this" value.If this parameter is empty, "undefined" is passed to the "this" value |
Explain
forEach() does not perform callback functions on empty arrays.
Code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Multiply all values in the array by a specific number:</title> </head> <body> <p>Click the button to multiply all the values in the array by a specific number.</p> <p>Multiply: <input type="number" id="inp" value="10"></p> <button onclick="num.forEach(myFunction)">Click on me</button> <p>Calculated value: <span id="demo"></span></p> <script> var num = [10, 20, 30, 40]; function myFunction(item,index,arr) { arr[index] = item * document.getElementById("inp").value; demo.innerHTML = numbers; } </script> </body> </html> </script>
13, map(): Returns a new array with elements that are the values of the original array elements after calling the function.
grammar
array.map(function(currentValue,index,arr), thisValue)
Explain
map() does not detect empty arrays.
map() does not change the original array.
Parameter details
parameter | describe |
---|---|
function(currentValue, index,arr) | Must.Function, which is executed by every element in the array |
currentValue (parameter in function) | Required.Current element. |
index (parameter within function) | Optional.The index value of the current element. |
arr (parameter in function) | Optional.Array object to which the current element belongs. |
thisValue | Optional.The object is used as the execution callback, passed to the function, and used as the value of "this".If thisValue is omitted or null or undefined is passed in, the this of the callback function is a global object. |
Code
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>map()</title> </head> <body> <p>Click the button to multiply each element of the array by the value specified in the input box and return to the new array.</p> <p>Multiply: <input type="number" id="inp" value="10"></p> <button onclick="myFunction()">Click on me</button> <p>New Array: <span id="arr"></span></p> <script> var number = [1, 2, 3, 4]; function multiplyArrayElement(num) { return num * document.getElementById("inp").value; } function myFunction() { document.getElementById("arr").innerHTML = number.map(multiplyArrayElement); } </script> </body> </html>
Reference
Newbie Tutorial: https://www.runoob.com
W3school: https://www.w3school.com.cn