A RetroSearch Logo

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

Search Query:

Showing content from https://developer.cdn.mozilla.net/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/toSorted below:

Array.prototype.toSorted() - JavaScript | MDN

Array.prototype.toSorted()

Baseline 2023

Newly available

Array 인스턴스의 toSorted() 메서드는 sort()에 대응되는 복사 버전의 메서드입니다. 이 메서드는 요소들을 오름차순으로 정렬한 새로운 배열을 반환합니다.

구문
toSorted()
toSorted(compareFn)
매개변수
compareFn Optional

요소 순서를 결정하는 함수입니다. 생략하면 배열 요소는 문자열로 변환되고 각 문자의 유니코드 코드 포인트 값에 따라 정렬됩니다. 자세한 정보는 sort()를 참고하시기 바랍니다.

반환 값

요소들을 오름차순으로 정렬한 새로운 배열입니다.

설명

compareFn 매개변수에 대해 더 알아보려면 sort()를 참조하세요.

희소 배열에서 toSorted() 메서드는 빈 슬롯을 undefined 값으로 간주하고 순회합니다.

toSorted() 메서드는 범용 메서드 입니다. this 값이 length 속성과 정수 키 속성을 가지고 있다고 가정합니다.

예제 배열 정렬하기
const months = ["Mar", "Jan", "Feb", "Dec"];
const sortedMonths = months.toSorted();
console.log(sortedMonths); // ['Dec', 'Feb', 'Jan', 'Mar']
console.log(months); // ['Mar', 'Jan', 'Feb', 'Dec']

const values = [1, 10, 21, 2];
const sortedValues = values.toSorted((a, b) => a - b);
console.log(sortedValues); // [1, 2, 10, 21]
console.log(values); // [1, 10, 21, 2]

더 많은 사용 예제는 sort()를 참조하세요.

희소 배열에서 toSorted() 사용하기

빈 슬롯은 undefined 값으로 간주되어 정렬됩니다. 빈 슬롯은 항상 배열의 끝으로 정렬되며 compareFn은 호출되지 않습니다.

console.log(["a", "c", , "b"].toSorted()); // ['a', 'b', 'c', undefined]
console.log([, undefined, "a", "b"].toSorted()); // ["a", "b", undefined, undefined]
배열이 아닌 객체에서 toSorted() 호출하기

toSorted() 메서드는 this의 length 속성을 읽습니다. 그런 다음 0부터 length - 1까지의 범위에 있는 모든 정수 키 속성을 수집하여 정렬하고 새 배열에 씁니다.

const arrayLike = {
  length: 3,
  unrelated: "foo",
  0: 5,
  2: 4,
  3: 3, // length가 3이기 때문에 toSorted()는 이를 무시합니다
};
console.log(Array.prototype.toSorted.call(arrayLike));
// [4, 5, undefined]
명세서 브라우저 호환성 같이 보기

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