README.md (4473B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # indexOf 22 23 > Return the first index at which a given element can be found. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var indexOf = require( '@stdlib/utils/index-of' ); 31 ``` 32 33 #### indexOf( arr, searchElement\[, fromIndex] ) 34 35 Returns the first index at which a given element can be found. 36 37 ```javascript 38 var arr = [ 4, 3, 2, 1 ]; 39 40 var idx = indexOf( arr, 3 ); 41 // returns 1 42 ``` 43 44 If a `searchElement` is **not** present in an input `array`, the function returns `-1`. 45 46 ```javascript 47 var arr = [ 4, 3, 2, 1 ]; 48 49 var idx = indexOf( arr, 5 ); 50 // returns -1 51 ``` 52 53 By default, the implementation searches an input `array` starting from the first element. To start searching from a different element, specify a `fromIndex`. 54 55 ```javascript 56 var arr = [ 1, 2, 3, 4, 5, 2, 6 ]; 57 58 var idx = indexOf( arr, 2, 3 ); 59 // returns 5 60 ``` 61 62 If a `fromIndex` exceeds the input `array` length, the function returns `-1`. 63 64 ```javascript 65 var arr = [ 1, 2, 3, 4, 2, 5 ]; 66 67 var idx = indexOf( arr, 2, 10 ); 68 // returns -1 69 ``` 70 71 If a `fromIndex` is less than `0`, the starting index is determined relative to the last index (with the last index being equivalent to `fromIndex = -1`). 72 73 ```javascript 74 var arr = [ 1, 2, 3, 4, 5, 2, 6, 2 ]; 75 76 var idx = indexOf( arr, 2, -4 ); 77 // returns 5 78 79 idx = indexOf( arr, 2, -1 ); 80 // returns 7 81 ``` 82 83 If `fromIndex` is less than `0` **and** its absolute value exceeds the input `array` length, the function searches the entire input `array`. 84 85 ```javascript 86 var arr = [ 1, 2, 3, 4, 5, 2, 6 ]; 87 88 var idx = indexOf( arr, 2, -10 ); 89 // returns 1 90 ``` 91 92 The first argument is not limited to `arrays`, but may be any [array-like][@stdlib/assert/is-array-like] `object`. 93 94 ```javascript 95 var str = 'bebop'; 96 97 var idx = indexOf( str, 'o' ); 98 // returns 3 99 ``` 100 101 </section> 102 103 <!-- /.usage --> 104 105 <section class="notes"> 106 107 ## Notes 108 109 - Search is performed using **strict equality** comparison. Thus, 110 111 ```javascript 112 var arr = [ 1, [ 1, 2, 3 ], 3 ]; 113 114 var idx = indexOf( arr, [ 1, 2, 3 ] ); 115 // returns -1 116 ``` 117 118 - This implementation is **not** [ECMAScript Standard][ecma-262] compliant. Notably, the [standard][ecma-262] specifies that an `array` be searched by calling `hasOwnProperty` (thus, for most cases, incurring a performance penalty), and the [standard][ecma-262] does **not** accommodate a `searchElement` equal to `NaN`. In this implementation, the following is possible: 119 120 ```javascript 121 // Locate the first element which is NaN... 122 var arr = [ 1, NaN, 2, NaN ]; 123 124 var idx = indexOf( arr, NaN ); 125 // returns 1 126 127 // Prototype properties may be searched as well... 128 function Obj() { 129 this[ 0 ] = 'beep'; 130 this[ 1 ] = 'boop'; 131 this[ 2 ] = 'woot'; 132 this[ 3 ] = 'bap'; 133 this.length = 4; 134 return this; 135 } 136 Obj.prototype[ 2 ] = 'bop'; 137 138 var obj = new Obj(); 139 140 idx = indexOf( obj, 'bop' ); 141 // returns -1 142 143 delete obj[ 2 ]; 144 145 idx = indexOf( obj, 'bop' ); 146 // returns 2 147 ``` 148 149 </section> 150 151 <!-- /.notes --> 152 153 <section class="examples"> 154 155 ## Examples 156 157 <!-- eslint no-undef: "error" --> 158 159 ```javascript 160 var indexOf = require( '@stdlib/utils/index-of' ); 161 162 var arr; 163 var obj; 164 var str; 165 var idx; 166 var i; 167 168 // Arrays... 169 arr = new Array( 10 ); 170 for ( i = 0; i < arr.length; i++ ) { 171 arr[ i ] = i * 10; 172 } 173 idx = indexOf( arr, 40 ); 174 175 console.log( idx ); 176 // => 4 177 178 // Array-like objects... 179 obj = { 180 '0': 'beep', 181 '1': 'boop', 182 '2': 'bap', 183 '3': 'bop', 184 'length': 4 185 }; 186 187 idx = indexOf( obj, 'bap' ); 188 189 console.log( idx ); 190 // => 2 191 192 // Strings... 193 str = 'beepboopbop'; 194 195 idx = indexOf( str, 'o' ); 196 197 console.log( idx ); 198 // => 5 199 ``` 200 201 </section> 202 203 <!-- /.examples --> 204 205 <section class="links"> 206 207 [ecma-262]: http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.indexof 208 209 [@stdlib/assert/is-array-like]: https://www.npmjs.com/package/@stdlib/assert-is-array-like 210 211 </section> 212 213 <!-- /.links -->