Baseline Widely available
indexOf()
㯠Array
ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ã弿°ã«ä¸ããããå
容ã¨åãå
容ãæã¤æåã®é
åè¦ç´ ã®æ·»åãè¿ãã¾ããåå¨ããªãå ´å㯠-1 ãè¿ãã¾ãã
const beasts = ["ant", "bison", "camel", "duck", "bison"];
console.log(beasts.indexOf("bison"));
// Expected output: 1
// Start from index 2
console.log(beasts.indexOf("bison", 2));
// Expected output: 4
console.log(beasts.indexOf("giraffe"));
// Expected output: -1
æ§æ
indexOf(searchElement)
indexOf(searchElement, fromIndex)
弿°
searchElement
æ¤ç´¢ããé åè¦ç´ ã§ãã
fromIndex
çç¥å¯
æ¤ç´¢ãå§ããä½ç½®ã®ã¼ãããå§ã¾ãã¤ã³ããã¯ã¹ã§ãæ´æ°ã«å¤æããã¾ãã
fromIndex < 0
ã®å ´åã fromIndex + array.length
ã使ç¨ããã¾ãããã ãããã®å ´åã§ãé
åã¯åããå¾ãã«åãã¦æ¤ç´¢ããã¾ããfromIndex < -array.length
ã¾ã㯠fromIndex
ãçç¥ãããå ´å㯠0
ã使ç¨ãããé
åå
¨ä½ã«å¯¾ãã¦æ¤ç´¢ãè¡ããã¾ããfromIndex >= array.length
ã®å ´åãé
åã®æ¤ç´¢ã¯è¡ãããã false
ãè¿ããã¾ããé
åå
ã«ããæåã® searchElement
ã®ã¤ã³ããã¯ã¹ã§ããè¦ã¤ãããªãã£ãå ´å㯠`-1`` ã§ãã
indexOf()
ã¡ã½ãã㯠searchElement
ã¨é
åã®è¦ç´ ãå³å¯ç価ï¼ä¸éã¤ã³ã¼ã«æ¼ç®å ===
ã§ä½¿ãããã®ã¨åãæ¹æ³ï¼ã使ã£ã¦æ¯è¼ãã¾ãã NaN
ã®å¤ã¯çããå¤ã¨ãã¦æ¯è¼ããããã¨ã¯ãªãã®ã§ãindexOf()
㯠searchElement
ã NaN
ã®ã¨ãã«ã¯å¸¸ã« -1
ãè¿ãã¾ãã
indexOf()
ã¡ã½ããã¯çé
åã®ç©ºã¹ããããã¹ããããã¾ãã
indexOf()
ã¡ã½ããã¯æ±ç¨çã§ãããã㯠this
å¤ã« length
ããããã£ã¨æ´æ°ãã¼ã®ããããã£ããããã¨ã ããæå¾
ãã¾ãã
以ä¸ã®ä¾ã¯ 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
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4