JS 依照元素出現數量排序
同樣是最近遇到的狀況,需要利用陣列內元素出現的數量進行排序,以下為範例程式:
var array = ["1", "2", "4", "5", "2", "4", "2", "3"]; var str = array.toString(); var filter = []; var result = []; document.write("原始陣列:" + array + "<br>"); var get = function (str, tar, arr, tmp) { if (str.indexOf(tar) >= 0) { tmp = str.slice(str.indexOf(tar) + 1); arr.push(tar); get(tmp, tar, arr, tmp); } return arr; } for (i in array) { var elm = []; var tmp; var fstr = filter.join(); if (fstr.indexOf(array[i]) >= 0) { continue; } else { var tmp_arr = get(str, array[i], elm, tmp); result.push(tmp_arr.length + ':' + tmp_arr[0]); filter.push(array[i]); } } result.sort(); result.reverse(); //按照出現次數排序,不刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); for (i = 0; i < show[0]; i++) { finishArray.push(show[1]); } } document.write("按出現次數排序,不刪除重複元素:" + finishArray + "<br>"); //按照出現次數排序,刪除重複元素 var finishArray = []; for (index in result) { var show = result[index].split(':'); finishArray.push(show[1]); } document.write("按出現次數排序,刪除重複元素:" + finishArray + "<br>");
輸出結果:
原始陣列:1,2,4,5,2,4,2,3 按出現次數排序,不刪除重複元素:2,2,2,4,4,5,3,1 按出現次數排序,刪除重複元素:2,4,5,3,1
cost of propecia without prescription: order cheap propecia prices – buy propecia pills
order generic propecia for sale cost of generic propecia without prescription buy cheap propecia online
antibiotics cipro ciprofloxacin generic ciprofloxacin order online
The 10 Most Terrifying Things About Online Grocery Stores That Ship online grocery Stores that ship
https://cytotec.club/# buy cytotec in usa
Here is my blog post: safest poker sites
http://lisinopril.network/# zestoretic 10 12.5
Heya i’m for the first time here. I came across this board
and I to find It really useful & it helped me out much.
I’m hoping to provide something back and help others such as
you helped me.
buy cytotec online fast delivery cytotec pills buy online buy cytotec over the counter
how does tamoxifen work: tamoxifen breast cancer – does tamoxifen cause joint pain
10 Facts About Replacement Car Key Cost That Insists On Putting You
In The Best Mood cheapest car key replacement (Gladis)
http://nolvadex.life/# tamoxifen for men
8 Tips To Enhance Your Double Glazing Lock Repair Game window repair [te.legra.ph]
Seven Reasons Why New Bmw Key Is Important Replacement Key For Bmw
Stilnoct is used for temporary sleep problems in adults that
cause severe anxiety or that affect everyday life.
should i take tamoxifen tamoxifen bone pain tamoxifen vs clomid
lisinopril 40 mg brand name in india zestril 10 zestoretic
I am not sure where you’re getting your information, but good topic.
I needs to spend some time learning much more or understanding more.
Thanks for wonderful information I was looking for this info for my mission.
buy cytotec in usa buy cytotec in usa cytotec abortion pill
Misoprostol 200 mg buy online buy cytotec pills cytotec pills buy online
12 Stats About Double Glazed Units Near Me To Make You Think Smarter About Other
People replacing a double glazed unit (Johnson)
Are You Confident About Doing Bunk Bed Price Uk? Check This Quiz
Bunk beds in sale
Why We Do We Love Autowatch Ghost Installers Near Me (And You Should Also!) Autowatch Ghost Installers Near Me
Where Are You Going To Find Leeds Windows And Doors One Year From
In The Near Future? near
Is Window Repair Near The Most Effective Thing That Ever
Was? window repair Near me (Mazafakas.com)
buy ciprofloxacin ciprofloxacin generic price ciprofloxacin over the counter
What’s The Job Market For Double Glazed Repairs Near Me Professionals?
double Glazed Repairs Near me
https://lisinopril.network/# order lisinopril 10 mg
tamoxifen side effects forum: how to prevent hair loss while on tamoxifen – tamoxifen
https://lisinopril.network/# lisinopril 420