A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf below:

Array.prototype.indexOf() - JavaScript | MDN

Array.prototype.indexOf()

Baseline Widely available

Array 인스턴스의 indexOf() 메서드는 배열에서 주어진 요소를 찾을 수 있는 첫 번째 인덱스를 반환하고, 찾을 수 없는 경우 -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 Optional

검색을 시작할 0 기반 인덱스로, 정수로 변환됩니다.

반환 값

배열에서 searchElement의 첫 번째 인덱스이고, 찾을 수 없으면 -1입니다.

설명

indexOf() 메서드는 엄격한 동등성을 사용하여 배열의 요소와 searchElement를 비교합니다(=== 연산자가 사용하는 것과 동일한 알고리즘). NaN 값은 절대 동일하게 비교되지 않으므로, searchElement가 NaN인 경우 indexOf()는 항상 -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(`새로운 veggies 컬력션은 ${veggies}`);
  } else {
    console.log(`${veggie}는 이미 veggies 컬렉션에 존재합니다.`);
  }
}

const veggies = ["potato", "tomato", "chillies", "green-pepper"];

updateVegetablesCollection(veggies, "spinach");
// 새로운 veggies 컬력션은 potato,tomato,chillies,green-pepper,spinach
updateVegetablesCollection(veggies, "spinach");
// spinach는 이미 veggies 컬렉션에 존재합니다.
희소배열에 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