current position:Home>JS JavaScript how to get the subscript of a value in the array

JS JavaScript how to get the subscript of a value in the array

2021-08-21 19:46:42 zdz8207

js The subscript of a value in the array javascript Know the value of an element in an array , How to get array subscripts JS Get the subscript of an element of the array Function method use prototype Prototype implementation , Find the index value of the element in the array js Find the position of the element in the array (index value ) Traverse all arrays , When the value in the array is the same as the value you expect, just return the subscript .

js The subscript of a value in the array
javascript Know the value of an element in an array , How to get array subscripts
JS Get the subscript of an element of the array Function method

use prototype Prototype implementation , Find the index value of the element in the array
js Find the position of the element in the array (index value )
Traverse all arrays , When the value in the array is the same as the value you expect, just return the subscript .

/*
*  Get the subscript of an element 
* arr:  The array passed in 
* obj:  The element that needs to get the subscript 
* */
function getArrayIndex(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] === obj) {
            return i;
        }
    }
    return -1;
}

// use prototype Prototype implementation , Find the index value of the element in the array 
Array.prototype.getArrayIndex=function(obj){
 for(var i=0;i<this.length;i++){
  if(this[i]===obj){
   return i;
     }
 }
    return -1;
}
var testarr = [1,2,3,4,5];
// obtain 2 stay testarr The subscript in is 1
testarr.getArrayIndex(2);
    
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
// Method to get the next subscript index of an element 
var obj = 2;
var arrlist = [1,2,3,4,5]; var index = getArrayIndex(arrlist,obj); var to = index+1; if(to > arrlist.length){ to = arrlist.length-1; }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

 

Expanding knowledge :
1, What is an array
An array is an ordered collection of values , Each value is called an element , And each element has a position in the array , In numbers , Known as the index .\
js The array of is typeless , Array elements can be of any type , Different elements in the same array may be objects or arrays .
The indexes of array elements do not have to be continuous , There can be gaps between elements , It's called a sparse array .
For example, sparse arrays var c = [1,,2]; The maximum index is 2, The array length is 3.
Each array has a length attribute . For non sparse arrays ,length Property is the number of array elements ,
For sparse arrays , Elemental length Attribute is larger than the index of all elements .

 

nature , Floating wind ,QQ Group : python Technology exchange group :453879716, AI deep learning group :251088643
golang Technology exchange group :316397059,vuejs Technology exchange group :458915921 Infiltration technology :534096581, Money hoarders :621258209, You can join me if you are interested
WeChat official account : Nature predicts (ssqyuce) Heart Zen (xinchandao)
 
 
 
 

copyright notice
author[zdz8207],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210821194639242Q.html

guess what you like

Random recommended