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
What Freud Can Teach Us About Marc Jacobs Handbag Black marc jacobs tote bag small
buy generic propecia without a prescription buy generic propecia cheap propecia without dr prescription
ciprofloxacin generic price: buy cipro – ciprofloxacin generic price
buy generic ciprofloxacin buy cipro cheap ciprofloxacin generic price
See What Treadmill Home Gym Tricks The Celebs Are Using Treadmill home
8 Tips To Improve Your Veterans Disability Lawsuit Game Veterans disability Law Firm
get generic propecia pill cost of propecia pills generic propecia no prescription
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You clearly know what youre talking about, why throw away your
intelligence on just posting videos to your site when you could be giving us something informative to read?
Nine Things That Your Parent Taught You About SEO Tools Search Engine Software seo tools search engine software (Hildred)
cytotec pills buy online Abortion pills online buy cytotec over the counter
ciprofloxacin generic price: cipro generic – buy cipro cheap
tamoxifen cyp2d6 where to buy nolvadex tamoxifen for men
tamoxifen depression: does tamoxifen make you tired – tamoxifen chemo
Why All The Fuss About Avon Spray Skin So Soft? avon Spray skin so soft
It’s in fact very complicated in this full of activity life to
listen news on TV, thus I simply use web for that reason, and
obtain the most recent information.
7 Tips To Make The Most Out Of Your CSGO Cases Opening
web Case
The Three Greatest Moments In Double Glazed Units Manufacturers
Near Me History Double Glazed Windows Repairs
10 Best Books On Tread Mills treadmill
9 Lessons Your Parents Taught You About Saab Key Programming Near Me saab
key programming near me (Eldon)
http://cytotec.club/# buy cytotec
http://lisinopril.network/# prinivil coupon
http://nolvadex.life/# tamoxifen and antidepressants
buy propecia tablets buy cheap propecia tablets order generic propecia without dr prescription
Where Will Personal Injury Claim Be One Year From What Is Happening Now?
firms
http://ciprofloxacin.tech/# cipro ciprofloxacin
propecia generics: cost of generic propecia – get cheap propecia tablets
cipro: where can i buy cipro online – cipro
Responsible For The Repairing Upvc Windows Budget? 12 Tips On How To Spend Your Money upvc Window Repair
buy cheap propecia online: get cheap propecia without a prescription – buy propecia without prescription
10 Things That Your Family Taught You About Replace Window Handles Replace Window handles