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
reputable mexican pharmacies online pharmacies in mexico that ship to usa mexican online pharmacies prescription drugs
mexican drugstore online: п»їbest mexican online pharmacies – purple pharmacy mexico price list
The History Of Private Psychiatrist Assessment Psychiatrist private
п»їbest mexican online pharmacies purple pharmacy mexico price list buying from online mexican pharmacy
pharmacies in mexico that ship to usa: buying prescription drugs in mexico – mexico drug stores pharmacies
See What Folding Treadmill Incline Tricks The Celebs Are Utilizing folding Treadmill incline
mexico drug stores pharmacies: п»їbest mexican online pharmacies – buying from online mexican pharmacy
Keep An Eye On This: How Key Programmers Is Taking Over And What We Can Do About
It key programmers near me
Just want to say your article is as surprising. The clarity
on your post is simply nice and i can suppose you’re a professional on this subject.
Well along with your permission allow me to seize your feed to stay updated with impending post.
Thanks 1,000,000 and please keep up the gratifying work.
buying prescription drugs in mexico online: buying prescription drugs in mexico – mexican pharmaceuticals online
Hi, ϳust wɑnteԀ to mention, I enjoʏed this blog post.
It was helpful. Keeep on posting!
Take a look at my page; porn
Why Nobody Cares About Upvc Patio Doors Upvc Replacement Door Panels
15 Replacement Upvc Window Handles Benefits Everyone Needs To Know upvc window repairs Near me
п»їbest mexican online pharmacies: mexican border pharmacies shipping to usa – mexican border pharmacies shipping to usa
For Whom Is Kids Bunk Beds And Why You Should Care
bunk bed uk
buying prescription drugs in mexico best online pharmacies in mexico reputable mexican pharmacies online
Five Killer Quora Answers To Treadmills Sale Treadmills sale
How Adding A Glass Window Replacement Near Me To Your Life Can Make All
The Different Replacement Glass For Double Pane Windows
Having read this I believed it was extremely informative.
I appreciate you finding the time and energy to put this information together.
I once again find myself spending a significant amount of time both reading and leaving comments.
But so what, it was still worth it!
mexican drugstore online mexico drug stores pharmacies mexican rx online
mexican pharmaceuticals online: best online pharmacies in mexico – buying prescription drugs in mexico
Can Slot Demo Gratis Never Rule The World? Slot Demo Spadegaming
https://ya-4545-25.s3.us-east-005.backblazeb2.com/company-registration-and-innovation-management.html
We carry brands that excel in mom of the bride jacket dresses, capes and pantsuits, like Alex Evenings, R&M Richards and Ignite.
pharmacies in mexico that ship to usa: buying prescription drugs in mexico online – mexico drug stores pharmacies
pharmacies in mexico that ship to usa mexican online pharmacies prescription drugs mexican drugstore online
What Is The Reason Why Window Glass Repair Near
Me Are So Helpful When COVID-19 Is In Session Double glazed window repairs near me
mexican pharmaceuticals online: mexican rx online – reputable mexican pharmacies online
purple pharmacy mexico price list: mexico pharmacies prescription drugs – mexican online pharmacies prescription drugs
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/bogin42/research/bogin42-(142).html
Give them glitz and glam on this allover sequin stunner.
I have been exploring for a bit for any high quality articles or weblog posts on this kind of
area . Exploring in Yahoo I eventually stumbled upon this site.
Studying this information So i’m glad to exhibit that I have a
very excellent uncanny feeling I discovered exactly what I needed.
I most for sure will make sure to do not fail to remember this website and provides it a look regularly.