JavaScript/methods (10) 썸네일형 리스트형 [JavaScript] 배열 정렬 Array.prototype.sort() sort 함수는 원본 배열을 직접 변경하며, 정렬된 배열을 반환한다. 기본 사용법은 아래와 같다1. array.sort( )2. array.sort( 비교함수 ) 배열의 요소를 정렬하고, 기본적으로 요소를 문자열로 변환한 후 유니코드 순서로 정렬한다.이 말은 숫자비교가 제대로 안된다는 뜻이다const numbers = [10, 1, 21];numbers.sort();console.log(numbers); // 출력: [1, 10, 21] 1. 배열 안의 숫자 비교 위 예에서 본 것처럼 숫자 비교를 할 시, 항상 비교함수를 넣어줘야 한다. 숫자배열.sort((a, b) => a - b); // 오름차순 숫자배열.sort((a, b) => b - a); // 내림차순 10 - 1 = 9 .. [JavaScript] Array.prototype.includes() includes() 메서드는 배열이 특정 요소를 포함하고 있는지 판별합니다.리턴 값은 Boolean 입니다. const array1 = [1, 2, 3];console.log(array1.includes(2));// Expected output: trueconst pets = ['cat', 'dog', 'bat'];console.log(pets.includes('cat'));// Expected output: trueconsole.log(pets.includes('at'));// Expected output: false참고로 string 타입에서도 쓸 수 있는 메서드 입니다.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_.. 이전 1 2 다음