Array.prototype.indexOf()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
indexOf() 㯠Array ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ã弿°ã«ä¸ããããå
容ã¨åãå
容ãæã¤æåã®é
åè¦ç´ ã®æ·»åãè¿ãã¾ããåå¨ããªãå ´å㯠-1 ãè¿ãã¾ãã
試ãã¦ã¿ã¾ããã
const beasts = ["ant", "bison", "camel", "duck", "bison"];
console.log(beasts.indexOf("bison"));
// äºæ³ãããçµæ: 1
// Start from index 2
console.log(beasts.indexOf("bison", 2));
// äºæ³ãããçµæ: 4
console.log(beasts.indexOf("giraffe"));
// äºæ³ãããçµæ: -1
æ§æ
indexOf(searchElement)
indexOf(searchElement, fromIndex)
弿°
searchElement-
æ¤ç´¢ããé åè¦ç´ ã§ãã
fromIndexçç¥å¯-
æ¤ç´¢ãå§ããä½ç½®ã®ã¼ãããå§ã¾ãã¤ã³ããã¯ã¹ã§ãæ´æ°ã«å¤æããã¾ãã
- ã¤ã³ããã¯ã¹ãè² ã®å ´åãé
åã®æ«å°¾ããããã®ã¼ã£ã¦æ°ãã¾ãã
-array.length <= fromIndex < 0ã®å ´åãfromIndex + array.lengthã使ç¨ããã¾ãããã ãããã®å ´åã§ãé åã¯åããå¾ãã«åãã¦æ¤ç´¢ããã¾ãã fromIndex < -array.lengthã¾ãã¯fromIndexãçç¥ãããå ´åã¯0ã使ç¨ãããé åå ¨ä½ã«å¯¾ãã¦æ¤ç´¢ãè¡ããã¾ããfromIndex >= array.lengthã®å ´åãé åã®æ¤ç´¢ã¯è¡ãããã-1ãè¿ããã¾ãã
- ã¤ã³ããã¯ã¹ãè² ã®å ´åãé
åã®æ«å°¾ããããã®ã¼ã£ã¦æ°ãã¾ãã
è¿å¤
é
åå
ã«ããæåã® searchElement ã®ã¤ã³ããã¯ã¹ã§ããè¦ã¤ãããªãã£ãå ´å㯠`-1`` ã§ãã
解説
indexOf() ã¡ã½ãã㯠searchElement ã¨é
åã®è¦ç´ ãå³å¯ç価ï¼ä¸éã¤ã³ã¼ã«æ¼ç®å === ã§ä½¿ãããã®ã¨åãæ¹æ³ï¼ã使ã£ã¦æ¯è¼ãã¾ãã NaN ã®å¤ã¯çããå¤ã¨ãã¦æ¯è¼ããããã¨ã¯ãªãã®ã§ãindexOf() 㯠searchElement ã NaN ã®ã¨ãã«ã¯å¸¸ã« -1 ãè¿ãã¾ãã
indexOf() ã¡ã½ããã¯çé
åã®ç©ºã¹ããããã¹ããããã¾ãã
indexOf() ã¡ã½ããã¯æ±ç¨çã§ãããã㯠this å¤ã« length ããããã£ã¨æ´æ°ãã¼ã®ããããã£ããããã¨ã ããæå¾
ãã¾ãã
ä¾
>indexOf() ã®ä½¿ç¨
以ä¸ã®ä¾ã¯ indexOf() ã使ã£ã¦ãé
åä¸ã®ããå¤ã®ä½ç½®ãæ¢ãã¦ãã¾ãã
const array = [2, 9, 9];
array.indexOf(2); // 0
array.indexOf(7); // -1
array.indexOf(9, 2); // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0
indexOf() ã使ã£ã¦ NaN ãæ¤ç´¢ãããã¨ã¯ã§ãã¾ããã
const array = [NaN];
array.indexOf(NaN); // -1
ããè¦ç´ ã®åå¨ããã¹ã¦è¦ã¤ãã
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.indexOf(element);
while (idx !== -1) {
indices.push(idx);
idx = array.indexOf(element, idx + 1);
}
console.log(indices);
// [0, 2, 4]
è¦ç´ ãé åå ã«åå¨ãããã©ããã調ã¹ãé åãæ´æ°ãã
function updateVegetablesCollection(veggies, veggie) {
if (veggies.indexOf(veggie) === -1) {
veggies.push(veggie);
console.log(`New veggies collection is: ${veggies}`);
} else {
console.log(`${veggie} already exists in the veggies collection.`);
}
}
const veggies = ["potato", "tomato", "chillies", "green-pepper"];
updateVegetablesCollection(veggies, "spinach");
// New veggies collection is: potato,tomato,chillies,green-pepper,spinach
updateVegetablesCollection(veggies, "spinach");
// spinach already exists in the veggies collection.
çé åã§ã® indexOf() ã®ä½¿ç¨
çé
åã®ç©ºã®ã¹ããããæ¤ç´¢ããããã« indexOf() ã使ç¨ãããã¨ã¯ã§ãã¾ããã
console.log([1, , 3].indexOf(undefined)); // -1
é åã§ã¯ãªããªãã¸ã§ã¯ãã«å¯¾ãã indexOf() ã®å¼ã³åºã
indexOf() ã¡ã½ãã㯠this ã® length ããããã£ãèªã¿è¾¼ã¿ã次ã«ãã¼ã length ããå°ããéè² ã®æ´æ°ã§ããåããããã£ã«ã¢ã¯ã»ã¹ãã¾ãã
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
3: 5, // length ã 3 ã§ãããã indexOf() ããç¡è¦ããã
};
console.log(Array.prototype.indexOf.call(arrayLike, 2));
// 0
console.log(Array.prototype.indexOf.call(arrayLike, 5));
// -1
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-array.prototype.indexof> |
ãã©ã¦ã¶ã¼ã®äºææ§
é¢é£æ å ±
Array.prototype.indexOfã®ããªãã£ã« (core-js)- es-shims ã«ãã
Array.prototype.indexOfã®ããªãã£ã« - ã¤ã³ããã¯ã¹ä»ãã³ã¬ã¯ã·ã§ã³ã®ã¬ã¤ã
ArrayArray.prototype.findIndex()Array.prototype.findLastIndex()Array.prototype.lastIndexOf()TypedArray.prototype.indexOf()String.prototype.indexOf()