는 요소가 순서상 같다고 간주하고 순서를 바꾸지 않습니다. 이를 응용하면 알파벳 순으로 정렬하면서
k
로 시작하는 단어만 원래 순서를 유지한다는 식의 응용이 가능합니다. 즉,
k
로 시작하는 단어는
j
로 시작하는
어떤 단어보다 뒤에 있고
l
로 시작하는 어떤 단어보다 앞에 있지만,
k
로 시작하는 단어들은 순서를 그대로 유
지하는 겁니다.
8.3
배열 검색
배열 안에서 뭔가 찾으려 할 때는 몇 가지 방법이 있습니다.
indexOf
부터 알아봅시다.
indexOf
는 찾고자 하는 것과 정확히 일치(
===
)하는 첫 번째 요소의 인덱스를 반환합니다.
indexOf
의
짝인
lastIndexOf
는 배열의 끝에서부터 검색합니다. 배열의 일부분만 검색하려면 시작 인덱
스를 지정할 수 있습니다.
indexOf
와
lastIndexOf
는 일치하는 것을 찾지 못하면
-
1
을 반환합
니다.
const
o
= {
name
: "
Jerry
" };
const
arr
= [
1
,
5
, "
a
",
o
,
true
,
5
, [
1
,
2
], "
9
"];
arr
.
indexOf
(
5
); //
1
arr
.
lastIndexOf
(
5
); //
5
arr
.
indexOf
("
a
"); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.
O’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
I wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
I’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
I'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.