Baseline 2023
Newly available
toSorted()
㯠Array
ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ã sort()
ã¡ã½ããã«å¯¾å¿ããã³ãã¼ã¡ã½ããã§ããããã¯ãè¦ç´ ãæé ã«ã½ã¼ãããæ°ããé
åãè¿ãã¾ãã
toSorted()
toSorted(compareFn)
弿°
compareFn
çç¥å¯
ã½ã¼ãé ãå®ç¾©ãã颿°ãæå®ãã¾ããçç¥ããå ´åã¯ãé åã®è¦ç´ ãæååã«å¤æãã Unicode ã³ã¼ããã¤ã³ãã®å¤ã«å¾ã£ã¦ä¸¦ã¹æ¿ãã¾ãã
a
æ¯è¼ããæåã®è¦ç´ ã§ãã
b
æ¯è¼ãã 2 çªç®ã®è¦ç´ ã§ãã
è¦ç´ ãæé ã«ã½ã¼ãããæ°ããé åã§ãã
解説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()
ãåç
§ãã¦ãã ããã
空ã®ã¹ãããã¯å¤ã 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
ããããã£ãèªã¿åãã¾ããããã¦ãlength - 1
ãã 0
ã¾ã§ã®æ´æ°ã®ãã¼ãæã¤åããããã£ãéé ã«è¨ªããç¾å¨ã®ããããã£ã®å¤ãé
åã®æ«å°¾ã«è¿½å ãã¦è¿ãã¾ãã
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