Recent Posts
Recent Comments
Link
Today
Total
02-08 14:03
관리 메뉴

채린씨의 티스토리

31. 배열 탐색 - 변형 본문

자료구조, 알고리즘/JavaScript 기초 문법

31. 배열 탐색 - 변형

채린씨 2022. 3. 12. 13:30

* 배열 탐색

- index 탐색(앞에서부터): Array.indexOf(item, from)

- index 탐색(뒤에서부터): Array.lastIndexOf(item, from)

- 값 포함 여부 확인: Array.includes(item, from)

let fruits = ["apple", "orange", "banana", "orange", "melon"];

console.log(fruits.indexOf("orange"));
// output: 1 (맨 앞에서부터 차례로 fruits 배열을 탐색, "orange"가 처음으로 등장하는 인덱스 값 반환)
console.log(fruits.indexOf("orange", 2));
// output: 3 (2번 인덱스 "banana"부터 차례로 fruits 배열을 탐색,
// "orange"가 처음으로 등장하는 인덱스 값 반환)
console.log(fruits.indexOf("kiwi"));
// output: -1 (맨 앞에서부터 차례로 fruits 배열을 탐색, "kiwi는 끝까지 등장하지 않으므로 -1 반환)

console.log(fruits.lastIndexOf("orange"));
// output: 3 (맨 뒤에서부터 fruits 배열을 탐색, "orange"가 처음으로 등장하는 인덱스 값 반환)
console.log(fruits.lastIndexOf("orange", -3));
// output: 1 (-3번 인덱스 "banana"부터 뒤에서 앞으로 fruits 배열을 탐색,
// "orange"가 처음으로 등장하는 인덱스 값 반환)
console.log(fruits.lastIndexOf("orange", 0));
// output: -1 (0번 인덱스 "apple"부터 뒤에서 앞으로 fruits 배열을 탐색,
// "apple" 앞에서는 "orange"가 등장하지 않으므로 -1 반환)

console.log(fruits.includes("banana"));
// output: true (fruits 배열에 "banana"가 등장하므로 true 반환)
console.log(fruits.includes("kiwi"));
// output: false (fruits 배열에 "kiwi"가 등장하지 않으므로 false 반환)
console.log(fruits.includes("Banana"));
// output: false (fruits 배열에 "Banana"가 등장하므로 true 반환, 대소문자를 구분함!!!)

 

* 배열 정렬/반전

- 배열 정렬: Array.sort()

- 배열 반전: Array.reverse()

let nums = [1, -1, 4, 5, 2, 0];
console.log(nums.reverse());
// output: [ 0, 2, 5, 4, -1, 1 ] (현재 배열 순서 뒤집기, 내림차순 정렬 아님!!!)
console.log(nums.sort());
// output: [ -1, 0, 1, 2, 4, 5 ] (오름차순 정렬)
console.log(nums.reverse());
// output: [ 5, 4, 2, 1, 0, -1 ] (현재 배열 순서 뒤집기, 내림차순 정렬 아님!!!)
// reverse()는 배열의 요소를 내림차순으로 정렬하는 메서드가 아니라, 현재 배열의 순서를 반대로 뒤집는 메서드!!!
// 여기서는 sort()로 오름차순 정렬을 해주고 다시 reverse()를 적용했으므로, 배열의 요소들이 내림차순으로 정렬됨
console.log(nums);
// output: [ 5, 4, 2, 1, 0, -1 ] (원래 배열 자체가 달라짐)

let fruits = ["apple", "orange", "Banana", "melon"];
console.log(fruits.reverse());
// output: [ 'melon', 'Banana', 'orange', 'apple' ] (현재 배열 순서 뒤집기, 내림차순 정렬 아님!!!)
console.log(fruits.sort());
// output: [ 'Banana', 'apple', 'melon', 'orange' ] (정렬, 대소문자 구분!!!)
// 각 문자열의 문자를 하나씩 비교하되, ASCII코드 순서대로 정렬.
console.log(fruits);
// output: output: [ 'Banana', 'apple', 'melon', 'orange' ] (원래 배열 자체가 달라짐)

 

* 배열 변환

- 배열 값을 문자열로 변환: Array.join(separator)

let fruits = ["apple", "orange", "banana", "melon"];

let str = fruits.join();
// fruits 배열의 요소를 ,로 이어붙인 문자열 생성
console.log(str);
// output: apple,orange,banana,melon

let str2 = fruits.join("!");
// fruits 배열의 요소를 !로 이어붙인 문자열 생성
console.log(str2);
// output: apple!orange!banana!melon

let result = str2.split("!");
// str2 문자열을 !단위로 구분해서 result 배열에 저장
console.log(result);
// output: [ 'apple', 'orange', 'banana', 'melon' ]

 

 

 

'자료구조, 알고리즘 > JavaScript 기초 문법' 카테고리의 다른 글

[JavaScript] 33. 그 외 고차함수  (1) 2022.03.13
[JavaScript] 32. 고차함수  (0) 2022.03.13
30. 배열 조작(!)  (0) 2022.03.12
29. Array  (0) 2022.03.09
28. 문자열 변환  (0) 2022.03.07
Comments