2026年5月11日 星期一

再探 Array.indexof()

 再探 Array.indexof()

前言

  在先前的 關於 Array.indexof() 介紹基本用法,發現未介紹它其實可以像 關於 String.indexOf() 指定從某個位址開始找,並不一定要找第一個,在此把學習的過程做個紀錄。


內容

  範例如下

const array = [2, 9, 3, 4, 9, 8 ];
console.log( array.indexOf( 9 ) );//1
console.log( array.indexOf( 9 , 2 ) );//4
console.log( array.indexOf( 9 , 99 ) );//-1


用法和  關於 String.indexOf() 一樣,只要透過第二個引數來指定開始找的索引,範例開始依舊方法找第一個,第二例就指定從 2 開始找,所以找到的是第二個 9 的索引,這個指定所以是可以大於陣列的大小的,只是結果一定是 -1 (找不到) 。

 

參考資料

[ developer.mozilla.org ] Array.prototype.indexOf()


相關文章與資料

關於 String.indexOf()

關於 Array.includes()

關於 Array.indexof()

2026年5月4日 星期一

關於 String.localeCompare() 的排序結果

 關於 String.localeCompare() 的排序結果

前言

  在之前 再探 Array.toSorted() 會用到 String.localeCompare() 來排序字串,所以就來研究一下預設字串排序是怎麼排的,在此把學習的過程做個紀錄。


內容

  範例如下

let ar = ["BB", "b", 'c' , "a" , "B123",'1'];
//
console.log( 'a'.localeCompare( '1') );//1
console.log( 'a'.localeCompare( 'a') );//0
console.log( 'a'.localeCompare( 'b') );//-1
//
console.log( ar.toSorted( (a , b) => a.toLowerCase().localeCompare(b.toLowerCase() ) ) );
//[ '1', 'a', 'b', 'B123', 'BB', 'c' ]


String.localeCompare() 本身需要輸入被比較的字串當引數,並回傳三種結果,1 、0 與 -1 ,這結果機會是專為排序相關方法而設計,接著利用 Array.toSorted() 來觀察排序的結果,排序是按照每個字的 ASCII code來排 ,那結果的 'B123' 比 'c' 還要前方是怎麼回事呢?因為預設的比較方法有做 toLowerCase() 的關係,所以才會發生大小寫按順序,接著看到 'B123' 與 'BB' 怎麼排的,  'B123' 是被排在前方的!所以不是按照字串的長度來排,而是按照第二個字元,由於 'B123'  的第二個字元是數字所以被排在前方。所以 String.localeCompare() 的排序結果是根據第一個字元的 ASCII code 來比較,如果被比較方的 ASCII code 不一樣那就是結果 '1' 或 '-1' ,如果一樣就在比較第二字元的結果,如果還是一樣就繼續比較第三的資源依此類推,如果比較方發現結束字元那就往左,反之,被比較方發現結束字元就等於不用換,回傳 '0'。


參考資料

[ developer.mozilla.org ] Array.prototype.toSorted()

[ developer.mozilla.org ] String.prototype.localeCompare()


相關文章與資料

再探 Array.toSorted()

初探 Array.toSorted()


2026年4月27日 星期一

再探 Array.toSorted()

 再探 Array.toSorted()

前言

  在先前 初探 Array.toSorted() 介紹 Array.toSorted() ,Array.toSorted()可以支援更換比較方法,但實際使用後有一些根想的不一樣,在此把學習的過程做個紀錄。


內容

  範例如下

let ar = ["Jan", "Feb", "Mar", "Apr"];
let numAr = [ 1 , 3 , 2 , 4 ];
//
console.log( ar.toSorted() );//["Apr", "Feb", "Jan", "Mar"]
console.log( ar.toSorted( (a , b) => a.toLowerCase().localeCompare(b.toLowerCase() ) ) );//["Apr", "Feb", "Jan", "Mar"]
console.log( ar.toSorted( (a , b) => b.toLowerCase().localeCompare(a.toLowerCase() ) ) );//["Mar", "Jan", "Feb", "Apr"]
//

console.log( numAr.toSorted() );//[1, 2, 3, 4]
console.log( numAr.toSorted( ( a , b ) => a - b) );//[1, 2, 3, 4]
console.log( numAr.toSorted( ( a , b ) => b - a) );//[4, 3, 2, 1]


只要再輸入比較方法就可更換比較方法,不過根想得不太一樣,不一樣的是比較手段不是用大於小於,而是字串與數字有各自的預設的比較手段,範例都是先預設比較,在替代等效預設比較,再來替代成跟預設比較相反的結果。


參考資料

[ developer.mozilla.org ] Array.prototype.toSorted()


相關文章與資料

初探 Array.toSorted()

2026年4月20日 星期一

再探 Array.toSpliced()

 再探 Array.toSpliced()

前言

  在之前的 初探 Array.toSpliced() 介紹裁掉的功能,但 Array.toSpliced() 不只是裁掉的功能,還能做出插入的功能,在此把學習的功能做個紀錄。


內容

  範例如下

let ar = ["Jan", "Feb", "Mar", "Apr"];
//
console.log( ar.toSpliced( 2 , 2 , '1111') );//["Jan", "Feb", "1111"]
console.log( ar.toSpliced( 0 , 2 , '1111','2222') );//["1111", "2222", "Mar", "Apr"]
console.log( ar.toSpliced( 1 , 2 , '1111','2222') );//["Jan", "1111", "2222", "Apr"]
console.log( ar.toSpliced( 1 , 0 , '1111','2222','3333') );//["Jan", "1111", "2222", "3333", "Feb", "Mar", "Apr"]


要使用插入的功能,只要在兩個數字引數後以增加引數的方式個別填入藥插入的資料即可。第一例是從索引 2 刪除 2 個內容再插入 "1111",第二例就是插入兩個資料,索引 0 刪除 2 個內容後再插入內容,可以看到第一與第二個被刪除後插入了我們輸入的內容,第三立在原本陣列的中間刪除內容再插入。最後一例可以發現只要將第二的引數輸入 0 ,就等於是從輸入的索引開始插入內容,不會刪除內容。


參考資料

[ developer.mozilla.org ] Array.prototype.toSpliced()


相關文章與資料

初探 Array.toSorted()

初探 Array.toReversed()

初探 Array.toSpliced()


2026年4月13日 星期一

初探 Array.toSpliced()

 初探 Array.toSpliced()

前言

  Array 提供裁減內容的方法,而且不會改變原本 Array 的內容,也就是 Array.toSpliced() ,在此把學習的過程做個紀錄。


內容

  範例如下

let ar = ["Jan", "Feb", "Mar", "Apr"];
//
console.log( ar.toSpliced( 2 ) );//["Jan", "Feb"]
//
console.log( ar.toSpliced( 0 , 3 ) );//["Apr"]
console.log( ar.toSpliced( 0 , 0 ) );//["Jan", "Feb", "Mar", "Apr"]
console.log( ar.toSpliced( 1 , 2 ) );//["Jan", "Apr"]
console.log( ar.toSpliced( 4 , 1 ) );//["Jan", "Feb", "Mar", "Apr"]
//
console.log( ar );//["Jan", "Feb", "Mar", "Apr"]


Array.toSpliced() 當輸入只有一個引數時,功能是從引數(索引值)以後全部裁掉,所以範例只剩頭兩個。如果輸入兩個引數,意思是從引數0(索引值)開始裁減引數1(數量),所以範例 toSpliced( 0 , 3 ) 只剩最後一個內容, toSpliced( 0 , 0 ) 要注意到引數1(數量)可以是 0 ,所以結果等於不裁減,接著, toSpliced( 1 , 2 )  的結果是中間兩個內容被裁減,最後看到   toSpliced( 4 , 1 ) ,引數0(索引值)超出陣列的數量,這樣也等於沒裁減。要注意, Array.toSpliced() 是產生一個新的 Array ,所以 ar 的內容永遠不會改變。


參考資料

[ developer.mozilla.org ] Array.prototype.toSpliced()


相關文章與資料

初探 Array.toSorted()

初探 Array.toReversed()

2026年4月5日 星期日

初探 Array.toReversed()

 初探 Array.toReversed()

前言

  Array 提供直接將所有的內容倒過來的功能,也就是 Array.toReversed() ,在此把學習的過程做個紀錄。


內容

  範例如下

let ar = ["Jan", "Feb", "Mar", "Apr"];
console.log( ar.toReversed() );//["Apr", "Mar", "Feb", "Jan"]
console.log( ar );//["Jan", "Feb", "Mar", "Apr"]


Array.toReversed() 用起來的特性類似 初探 Array.toSorted() ,都是產生一個新的 Array 來得到結果,所以可以看到在 Array.toReversed() 後,原本的內容並不會改變。


參考資料

[ developer.mozilla.org ] Array.prototype.toReversed()


相關文章與資料

初探 Array.toSorted()

2026年3月30日 星期一

關於 Set.isSupersetOf()

 關於 Set.isSupersetOf()

前言

  在之前的 關於 Set.isSubsetOf() 介紹內容是否完全包含的檢查,不過是引數是否完全包含喚起的 Set ,也許用起來讓你覺得不直覺,所以有提供喚起的 Set 是否完全包含引數,也就是 Set.isSupersetOf() ,在此把學習的過程做個紀錄。


內容

  範例如下

let setA = new Set(['a','b','c','A']);
let setB = new Set(['b','c','d','e']);
let setC = new Set(['a','b','c','A','B','C']);
console.log( setC.isSupersetOf( setB ) );//flase
console.log( setC.isSupersetOf( setA ) );//true


這次與 Set.isSubsetOf() 不一樣,是喚起的 Set 是否完全包含引數的 Set ,所以喚起 Set 是 setC ,其餘的就和 Set.isSubsetOf() 的範例是一樣的。


參考資料

[ developer.mozilla.org ] Set


相關文章與資料

關於 JavaScript 的 Set

關於列印 Set 的內容

關於 Set.union()

關於 Set.difference()

關於 Set.intersection()

關於 Set.symmetricDifference()

關於 Set.isDisjointFrom()

關於 Set.isSubsetOf()