/* eslint-disable complexity */
|
/**
|
* @fileoverview Returns the first index at which a given element can be found in the array.
|
* @author NHN FE Development Lab <dl_javascript@nhn.com>
|
*/
|
|
'use strict';
|
|
var isArray = require('../type/isArray');
|
|
/**
|
* @module array
|
*/
|
|
/**
|
* Returns the first index at which a given element can be found in the array
|
* from start index(default 0), or -1 if it is not present.
|
* It compares searchElement to elements of the Array using strict equality
|
* (the same method used by the ===, or triple-equals, operator).
|
* @param {*} searchElement Element to locate in the array
|
* @param {Array} array Array that will be traversed.
|
* @param {number} startIndex Start index in array for searching (default 0)
|
* @returns {number} the First index at which a given element, or -1 if it is not present
|
* @memberof module:array
|
* @example
|
* // ES6
|
* import inArray from 'tui-code-snippet/array/inArray';
|
*
|
* // CommonJS
|
* const inArray = require('tui-code-snippet/array/inArray');
|
*
|
* const arr = ['one', 'two', 'three', 'four'];
|
* const idx1 = inArray('one', arr, 3); // -1
|
* const idx2 = inArray('one', arr); // 0
|
*/
|
function inArray(searchElement, array, startIndex) {
|
var i;
|
var length;
|
startIndex = startIndex || 0;
|
|
if (!isArray(array)) {
|
return -1;
|
}
|
|
if (Array.prototype.indexOf) {
|
return Array.prototype.indexOf.call(array, searchElement, startIndex);
|
}
|
|
length = array.length;
|
for (i = startIndex; startIndex >= 0 && i < length; i += 1) {
|
if (array[i] === searchElement) {
|
return i;
|
}
|
}
|
|
return -1;
|
}
|
|
module.exports = inArray;
|