配列の番号を返す

indexOf

indexOfが配列に使えないので、代わりに。

成功した場合は最初に見つかった位置のインデックス番号、見つからなかった場合は-1を返します。

var inArray = function(searchValue, arrayData) {
if (typeof searchValue === ‘undefined’ || typeof arrayData !== ‘object’) return -1;

for (var key in arrayData) {
if (arrayData[key] === searchValue) return key;
}

return -1;
};

 

 

 

配列要素の型を判定して、指定の型を持つ要素番号を返す(ES3互換)

 

// ES5 `Array.isArray()` Polyfill. from MDN. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}


// ES5 `Number.isInteger()` Polyfill. from MDN. https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger
if (!Number.isInteger) {
Number.isInteger = function isInteger (nVal) {
return typeof nVal === "number" && isFinite(nVal) && nVal > -9007199254740992 && nVal < 9007199254740992 && Math.floor(nVal) === nVal;
};
}


/**
* Find the array index of specific data type elements.
* This method returns the index of the matching specific data type elements.
* The matching rules can select a plurality of data types and specify them as array.
*
* @function
* @param {Array|String} elementDataTypes Matching rules. Array into one or more data type string.
* @param {Array} array The array to check.
* @param {Boolean} [isFull] If true, getting all matching index from the array.
* @param {Number} [fromIndex] Starting the search at fromIndex.
* @returns {Array|Number} Returns index if array contain the specify data type, else -1.
*/
function findIndexOfElementDataType(elementDataTypes, array, isFull, fromIndex) {
if (! Array.isArray(array) || array.length === 0) { return -1; }

var results = [],
arr = [],
types = Array.isArray(elementDataTypes) ? elementDataTypes : [elementDataTypes],
objType,
fromIndex = Number.isInteger(fromIndex) ? fromIndex : 0;

for (var i = 0; i < types.length; i++) {
types[i] = types[i].charAt(0).toUpperCase() + types[i].substring(1).toLowerCase();
}

if (isFull) {
for ( ; fromIndex < array.length; fromIndex++) {
if (array[fromIndex] === null) {
arr[fromIndex] = 'Null';

for (var i = 0; i < types.length; i++) {
if (arr[fromIndex] === types[i]) {
results.push(fromIndex);
}
}
} else if (typeof array[fromIndex] === 'undefined') {
arr[fromIndex] = 'Undefined';

for (var i = 0; i < types.length; i++) {
if (arr[fromIndex] === types[i]) {
results.push(fromIndex);
}
}
} else {
objType = Object.prototype.toString.call(array[fromIndex]);
objType = objType.slice(8, objType.length - 1);

for (var i = 0; i < types.length; i++) {
if (objType === types[i]) {
results.push(fromIndex);
}
}
}
}
} else {
for ( ; fromIndex < array.length; fromIndex++) {
if (array[fromIndex] === null) {
arr[fromIndex] = 'Null';

for (var i = 0; i < types.length; i++) {
if (arr[fromIndex] === types[i]) {
results.push(fromIndex);
return results;
}
}
} else if (typeof array[fromIndex] === 'undefined') {
arr[fromIndex] = 'Undefined';

for (var i = 0; i < types.length; i++) {
if (arr[fromIndex] === types[i]) {
results.push(fromIndex);
return results;
}
}
} else {
objType = Object.prototype.toString.call(array[fromIndex]);
objType = objType.slice(8, objType.length - 1);

for (var i = 0; i < types.length; i++) {
if (objType === types[i]) {
results.push(fromIndex);
return results;
}
}
}
}
}
return (results.length > 0) ? results : -1;
}
タイトルとURLをコピーしました