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
USP human growth hormone (somatropin).
My web-site – 1000mg cbd muscle & joint balm
What Is Big Chiminea And Why Is Everyone Dissing It?small chiminea (https://dailybookmarkhit.com/story18160491/24-hours-to-improve-ceramic-chiminea)
I am impressed with this site, rattling I am a big fan.
My blog post Pinterest
You’ll Never Be Able To Figure Out This American French Style Fridge Freezer’s Benefits French style fridge
What Will Land Rover Replacement Key Be Like In 100 Years?
land rover lr2 key fob stuck in ignition (Doug)
rybelsus cost: rybelsus pill – rybelsus price
перейдите на этот сайт Kraken20.at
Great article, just what I needed.
9 Things Your Parents Taught You About Mini Key Fob program mini key fob
Hello there, I discovered your web site by means of Google even as looking for a similar matter, your web
site got here up, it seems good. I have bookmarked it
in my google bookmarks.
Hi there, just changed into aware of your blog through Google,
and found that it is really informative. I am going to be careful for brussels.
I’ll appreciate if you happen to proceed this in future.
Numerous people might be benefited out of your writing.
Cheers!
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем: сервисные центры в волгограде
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Pragmatic Slots Return Rate Tools To Help You Manage Your Life Everyday 프라그마틱 데모,modernbookmarks.com,
Why Is Double Glaze Repair Near Me So Popular? Misted double Glazing
It’s very trouble-free to find out any matter on web as compared to books,
as I found this paragraph at this web page.
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем: сервис центры бытовой техники волгоград
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
The 9 Things Your Parents Teach You About L Shape Leather Sofa l shape leather sofa
This vaporizer has a number of unique functions.
Here is my homepage; does plant based meat taste the same
The Little-Known Benefits Wall Mounted Fireplace white wall mounted electric fire (Tahlia)
Five Killer Quora Answers On Best Low Bunk Beds bunk
The average cost to move an electric meter of $675.
Stop by my page :: compare energy plans victoria
Guide To Situs 4d: The Intermediate Guide On Situs 4d situs 4d
Best Kids Bunk Beds Tools To Improve Your Everyday Lifethe Only Best Kids Bunk Beds Trick
That Every Person Must Learn Best Kids Bunk Beds
Why We Our Love For French Door Fridge Freezer (And You Should Too!) french door Fridge not plumbed, images.Google.so,
What’s The Current Job Market For Upvc Patio Doors Professionals Like?
new upvc door (Stephania)
How Can A Weekly Freestanding Fireplace Electric Project Can Change Your Life electric log effect fires freestanding (Meredith)
Thanks for sharing your thoughts on click here.
Regards
What’s The Job Market For Key Programming Car Professionals Like?key programming car [Gail]
https://ozempic.art/# ozempic generic
How To Explain Replace Upvc Window Handle To A Five-Year-Old repairs to upvc windows (kring-kristiansen.blogbright.net)
You’ll Never Be Able To Figure Out This Upvc Window Repairs’s Tricks upvc window repairs (https://dale-demir.mdwrite.net)