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
My spouse and I stumbled over here by a different website and thought I may as
well check things out. I like what I see so now i am following
you. Look forward to going over your web page repeatedly.
12 Stats About Injury Litigation To Make You Think About The Other People key west injury Law firm
10 Reasons Why People Hate Shopping Online Sites List online shop (https://login.proxy.uwec.Edu/login?qurl=http://vimeo.com/931617050)
https://cialist.pro/# Generic Tadalafil 20mg price
15 Gifts For The Online Grocery Stores That Ship Lover In Your Life Fitness Equipment Pulley
15 Presents For Your Replacement Windows Leeds Lover In Your Life Window doctor leeds
Where Can You Get The Most Reliable Beko Washing Machines Uk Information?
An Easy-To-Follow Guide To Windows Repairs
Near Me window replacement
20 Resources That Will Make You Better At Uk Online Grocery Shopping
Sites Barista Cleaning Tablets
Levitra 20 mg for sale Levitra 20mg price Buy Vardenafil 20mg online
cheap kamagra kamagra.win п»їkamagra
Great delivery. Outstanding arguments. Keep up the great spirit.
Feel free to surf to my site Rx 2 Go Pharmacy
10 CSGO Cases Opening Related Projects That Can Stretch Your Creativity Web
Case (Lslv168.Com)
Wood Burner: 11 Thing You’re Not Doing wood burner Clearance Sale
The Comprehensive Guide To Upvc Window Repairs Upvc window repairs near me
Buy Vardenafil 20mg levitrav.store Vardenafil buy online
sildenafil over the counter Cheap Viagra 100mg sildenafil 50 mg price
Right now it looks like BlogEngine is the preferred blogging
platform available right now. (from what I’ve read) Is that what you’re using on your
blog?
Eu recomendo este cassino blaze apostas
buy kamagra online usa: kamagra pills – п»їkamagra
Where Will Online Shopping Clothes Uk Cheap Be 1 Year From Today?
Lucky Line Magnetic Key Box
Cheap Levitra online Buy generic Levitra online Buy Levitra 20mg online
https://cenforce.pro/# buy cenforce
I like what you guys tend to be up too. This type of clever work and exposure!
Keep up the good works guys I’ve you guys to my personal blogroll.
https://viagras.online/# Cheap generic Viagra
Viagra online price: Cheap Sildenafil 100mg – sildenafil over the counter
3 Ways The Online Shopping Uk Amazon Will Influence Your Life Mike & Ike 4.5 lb
Levitra tablet price: Levitra generic price – Levitra online USA fast
10 Things You’ve Learned From Kindergarden Which
Will Help You With Double Glazing Companies Near Me repairing double glazing Windows
The Top Reasons People Succeed At The CSGO Case Battle Sites Industry Cs2 case Opening