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
Are Classic Slots As Crucial As Everyone Says? slot features
The 10 Most Scariest Things About Modern Slots casino slot Games
It’s a pity you don’t have a donate button! I’d definitely donate
to this fantastic blog! I suppose for now i’ll settle
for bookmarking and adding your RSS feed to my Google account.
I look forward to brand new updates and will talk about this site with
my Facebook group. Talk soon!
Do You Think New Upvc Door Never Rule The World? replace lock on upvc Door
5 Cliches About Replacement Windows Birmingham You Should Avoid Upvc Windows Birmingham
What Is The Reason Bunk Beds For Sale Is The Right Choice For
You? Phillis
The Most Innovative Things Happening With Uk Online Phone Shopping Sites Tierdrop Paper Sorter
A certified Asbestos Lawsuit attorney can assist
victims and their families receive compensation. There are
limitations on time that are known as statutes of limitation within which asbestos victims can file claims.
Meds information. What side effects can this medication cause?
cost cheap linezolid prices
Actual trends of drug. Get here.
Solid SEO Tools Tips To Relax Your Daily Life Solid SEO Tools Trick Every Person Should Be
Able To Google seo tool
Online Shopping Website In London Tools To Ease Your Daily
Lifethe One Online Shopping Website In London Trick That
Every Person Must Learn online Shop
1win казино зеркало сайта
15 Collapsible Mobility Scooters Benefits Everyone Should Be Able To arlennizo.top
10 Key Factors About Shop Online Uk Women’s Fashion You Didn’t Learn In School
18000 Lbs Clevis Hook
Don’t Forget Slot Machines: 10 Reasons Why You Don’t
Have It slot machine bonuses (Tina)
What Freud Can Teach Us About Veterans Disability Law Veterans Disability Lawyers
There’s certainly a great deal to know about this issue.
I love all the points you’ve made.
7 Helpful Tricks To Making The Most Out Of Your Birth Defect Lawsuit attorneys
buy prednisone online overnight canada
dexamethasone 6 mg tablet
Howdy! where buy cheap finasteride without a prescription very good web site.
Hi, its pleasant paragraph concerning media print, we all
be familiar with media is a great source of data.
3 Reasons Three Reasons Your Search Engine Optimization Cost Is Broken (And How To
Repair It) local search optimisation service – Keith –
can you get cheap lyrica without insurance
What’s The Reason? Online Shopping Websites Clothes Is Everywhere This Year online shopping websites for Clothes
The 10 Scariest Things About Free Spin Slots free spin slots (Elizabeth)
Private ADHD Assessment Cost: What’s No One Has Discussed Private Adult Adhd Assessment Scotland
What Is Themed Slots And Why Is Everyone Talking About It?
themed Slot games (https://go.appsscript.info/videoslots892691)
See What Green Power Mobility Reviews Tricks The Celebs Are
Using Green Power Mobility reviews
Great web site you’ve got here.. It’s difficult
to find excellent writing like yours nowadays. I really appreciate
individuals like you! Take care!!