JavaScript中數組的合併以及排序實作範例_基礎知識
合併兩個陣列 - concat()
原始碼:
<!DOCTYPE html> <html> <body> ​ <p id="demo">点击按钮合并数组。</p> ​ <button onclick="myFunction()">点我</button> ​ <script> function myFunction() { var hege = ["Cecilie", "Lone"]; var stale = ["Emil", "Tobias", "Linus"]; var children = hege.concat(stale); var x=document.getElementById("demo"); x.innerHTML=children; } </script> ​ </body> </html>
測試結果:
Cecilie,Lone,Emil,Tobias,Linus
合併三個陣列 - concat()
原始碼:
<!DOCTYPE html> <html> <body> <script> var parents = ["Jani", "Tove"]; var brothers = ["Stale", "Kai Jim", "Borge"]; var children = ["Cecilie", "Lone"]; var family = parents.concat(brothers, children); document.write(family); </script> </body> </html>
測試結果:
Jani,Tove,Stale,Kai Jim,Borge,Cecilie,Lone
數組排序(按字母順序升序)- sort()
原始碼:
<!DOCTYPE html> <html> <body> ​ <p id="demo">Click the button to sort the array.</p> ​ <button onclick="myFunction()">Try it</button> ​ <script> function myFunction() { var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); var x=document.getElementById("demo"); x.innerHTML=fruits; } </script> ​ </body> </html>
測試結果:
Apple,Banana,Mango,Orange
數字排序(以數字順序升序)- sort()
原始碼:
<!DOCTYPE html> <html> <body> ​ <p id="demo">Click the button to sort the array.</p> ​ <button onclick="myFunction()">Try it</button> ​ <script> function myFunction() { var points = [40,100,1,5,25,10]; points.sort(function(a,b){return a-b}); var x=document.getElementById("demo"); x.innerHTML=points; } </script> ​ </body> </html>
測試結果:
1,5,10,25,40,100
數字排序(依數字順序降序)- sort()
原始碼:
<!DOCTYPE html> <html> <body> ​ <p id="demo">Click the button to sort the array.</p> ​ <button onclick="myFunction()">Try it</button> ​ <script> function myFunction() { var points = [40,100,1,5,25,10]; points.sort(function(a,b){return b-a}); var x=document.getElementById("demo"); x.innerHTML=points; } </script> ​ </body> </html>
測試結果:
100,40,25,10,5,1
將一個陣列中的元素的順序反轉排序 - reverse()
原始碼:
<!DOCTYPE html> <html> <body> ​ <p id="demo">Click the button to reverse the order of the elements in the array.</p> ​ <button onclick="myFunction()">Try it</button> ​ <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; ​ function myFunction() { fruits.reverse(); var x=document.getElementById("demo"); x.innerHTML=fruits; } </script> ​ </body> </html>
測試結果:
Mango,Apple,Orange,Banana

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題











使用foreach循環移除PHP數組中重複元素的方法如下:遍歷數組,若元素已存在且當前位置不是第一個出現的位置,則刪除它。舉例而言,若資料庫查詢結果有重複記錄,可使用此方法移除,得到不含重複記錄的結果。

PHP中深度複製數組的方法包括:使用json_decode和json_encode進行JSON編碼和解碼。使用array_map和clone進行深度複製鍵和值的副本。使用serialize和unserialize進行序列化和反序列化。

PHP數組鍵值翻轉方法效能比較顯示:array_flip()函數在大型數組(超過100萬個元素)下比for迴圈效能更優,耗時更短。手動翻轉鍵值的for迴圈方法耗時相對較長。

PHP的array_group_by函數可依鍵或閉包函數將陣列中的元素分組,傳回關聯數組,其中鍵為組名,值是屬於該組的元素數組。

在PHP中執行陣列深度複製的最佳實踐是:使用json_decode(json_encode($arr))將陣列轉換為JSON字串,然後再轉換回陣列。使用unserialize(serialize($arr))將陣列序列化為字串,然後將其反序列化為新陣列。使用RecursiveIteratorIterator迭代器對多維數組進行遞歸遍歷。

多維數組排序可分為單列排序和嵌套排序。單列排序可使用array_multisort()函數依列排序;巢狀排序需要遞歸函數遍歷陣列並排序。實戰案例包括按產品名稱排序和按銷售量和價格複合排序。

PHP的array_group()函數可用來按指定鍵對陣列進行分組,以尋找重複元素。函數透過以下步驟運作:使用key_callback指定分組鍵。可選地使用value_callback確定分組值。對分組元素進行計數並識別重複項。因此,array_group()函數對於尋找和處理重複元素非常有用。

PHP數組合併去重演算法提供了平行的解決方案,將原始陣列分成小塊並行處理,主進程合併區塊的結果去重。演算法步驟:分割原始數組為均等分配的小塊。並行處理每個區塊去重。合併區塊結果並再次去重。
