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
Abortion pills online: cytotec buy online usa – purchase cytotec
Because the admin of this web site is working, no doubt very shortly it will be famous, due to
its quality contents.
tamoxifen premenopausal: tamoxifen and weight loss – nolvadex for pct
The 9 Things Your Parents Teach You About Getting
A Car Key Cut getting a car Key cut
Five Killer Quora Answers On Treadmill Near Me Treadmill Near Me
A Intermediate Guide In Used Mobility Scooters For Sale invacare mobility scooters for sale
We’ve Had Enough! 15 Things About Car Accident Attorney We’re Tired Of Hearing car Accident Attorney near me
7 Things About Washing And Dryer Combo You’ll Kick Yourself For Not Knowing washing and Dryer combo
http://lisinopril.network/# prinivil medication
Hey There. I found your blog using msn. This is a really well written article.
I will be sure to bookmark it and come back to read
more of your useful info. Thanks for the post. I will certainly comeback.
Incredible loads of great tips.
The Little-Known Benefits Of Green Mobility electric power scooters
(Chloe)
Your Family Will Thank You For Getting This Washer Dryer Heat Pump
Heat Pump Washer Dryer
15 Funny People Working In Buy Beko Washing Machine In Buy Beko
washing machines beko (ns1.javset.net)
Machine
generic lisinopril: lisinopril tablets for sale – order cheap lisinopril
https://cytotec.club/# cytotec online
I’m amazed, I have to admit. Rarely do I encounter a blog that’s equally educative and interesting, and
let me tell you, you’ve hit the nail on the head. The problem is something which not enough folks
are speaking intelligently about. Now i’m very happy I stumbled across
this during my search for something relating to
this.
my blog post … Czech names
buy cytotec over the counter: cytotec online – order cytotec online
tamoxifen moa tamoxifen hip pain tamoxifen pill
https://finasteride.store/# buying propecia pill
7 Practical Tips For Making The Greatest Use Of Your Replacement
Vauxhall Key Vauxhall Car Keys (https://Peaceful-Giraffe-Hvp6Hh.Mystrikingly.Com)
Double Glazed Window Repair Near Me: What’s No One
Is Talking About double glazed units replacement (https://80adec2ampndbs9h.рф/user/enginetoe40)
Five Killer Quora Answers On Designer Handbags Large designer handbags large
(https://peatix.com/User/21728553)
price of zestril 30 mg: price for 5 mg lisinopril – zestoretic tabs
buy cipro online: buy cipro online without prescription – ciprofloxacin generic price
buying cheap propecia without prescription cost of propecia without insurance buying cheap propecia online
You’ll Be Unable To Guess Dangerous Drugs Lawsuit’s Tricks dangerous drugs lawsuit
Five Things You Don’t Know About Lost Keys For Car lost key what to do
What Is Car Accident Lawyer And How To Use What Is Car Accident Lawyer And
How To Use Car Crash Attorney Near Me
The Hidden Secrets Of Double Glazing Windows Near Me Double Glazed Panes