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
The Most Pervasive Issues With Veterans Disability Compensation veterans disability law Firms (Promisec.net)
The Most Pervasive Problems With Repair Double Glazing casement
https://cialist.pro/# Tadalafil price
order viagra Sildenafil 100mg price buy Viagra online
15 Best Twitter Accounts To Find Out More About Autowatch Ghost 2 Installers Near Me ferrari Ghost installer
This Is The Advanced Guide To Replacement Windows Leeds upvc Door repair meanwood
You’ll Never Guess This London Online Mobile Shopping Sites’s Tricks london online mobile Shopping sites
Three Reasons Why The Reasons For Your Cheap Online Grocery Shopping Uk Is Broken (And How To Fix It) Custom Wood Floor registers
Kamagra 100mg kamagra oral jelly buy kamagra online usa
cheap kamagra: kamagra – sildenafil oral jelly 100mg kamagra
What’s The Most Common Easy Work From Home Jobs Uk Debate Doesn’t Have To Be As Black Or White As You May Think
work from home jobs No experience (pattern-wiki.win)
My brother suggested I might like this web site. He was totally right. This post truly made my day. You cann’t imagine simply how much time I had spent for this info! Thanks!
11 Ways To Completely Revamp Your Online Shopping Websites For Clothes
Bonus Eraser Colored Pencils
cenforce.pro: buy cenforce – Cenforce 150 mg online
Searching For Inspiration? Try Looking Up London Online Clothing Shopping Sites
professional diving Fins atomic
Excellent article. Keep writing such kind of info on your site.
Im really impressed by it.
Hello there, You’ve done a fantastic job. I will definitely digg it and personally suggest to my
friends. I am confident they will be benefited from this site.
What’s The Job Market For Double Glazed Window Repairs
Professionals? double glazed window repairs (Meagan)
п»їBuy generic 100mg Viagra online Buy Viagra online Cheap generic Viagra
Are You Getting The Most Value From Your Columbia Mesothelioma Lawyer?
Houston Mesothelioma Lawyer
https://levitrav.store/# Levitra online USA fast
Viagra online price Cheapest place to buy Viagra Cheap Viagra 100mg
The 3 Biggest Disasters In Repair Double Glazed Windows The
Repair Double Glazed Windows’s 3 Biggest Disasters In History Window Repairs
My developer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the
costs. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites
for about a year and am nervous about switching to another platform.
I have heard fantastic things about blogengine.net.
Is there a way I can import all my wordpress content into it?
Any help would be really appreciated!
cheapest cialis: buy cialis online – Buy Cialis online
Seven Reasons To Explain Why Skin So Soft Is Important Skin So soft
http://cenforce.pro/# cenforce.pro
Who’s The World’s Top Expert On London Online Clothing Shopping
Sites? 2 Shelf Rubbermaid Transport Cart, Refugio,
The Best Way To Explain Mini Key Fob Replacement To Your
Boss replace mini key fob – peatix.com –
Vardenafil buy online: Levitra 10 mg best price – Levitra 20 mg for sale
The Biggest Issue With Asbestos Lawsuits And How You Can Resolve It Asbestos Case