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
Peculiar article, exactly what I needed.
Online Shopping Website In London Tools To Streamline Your Daily Lifethe One Online Shopping Website In London Technique Every Person Needs To Be Able To online shopping website in london
porn
Virtually all cbd gummies for ed for sale near me companies make a gummy product.
Thanks Joy Organics. cbd gummies sold in texas products tend to be expensive.
online slot
What’s The Current Job Market For Uk Women’s Online Shopping Websites Professionals
Like? uk women’s Online shopping websites
lgbt porn
The Most Inspirational Sources Of Window Repair Near Upvc Window Repair Near Me
Thanks Joy Organics. the best cbd gummies for stress and anxiety products tend to be expensive.
Keep this going please, great job!
The 10 Most Scariest Things About Uk Online Shopping Sites For
Electronics uk online shopping sites for electronics
What’s The Current Job Market For Window Doctor Near Me Professionals Like?
window doctor near me (Indira)
To connect with Joy Organics, log into Facebook.
Feel free to surf to my web-site: royal cbd gummies for sale near me
Not everyone wants THC in their CBD products.
My web page: Bookmarks
I’m amazed, I have to admit. Rarely do I encounter a blog that’s equally
educative and entertaining, and without a doubt,
you’ve hit the nail on the head. The issue is something that not enough people are
speaking intelligently about. I’m very happy that I
stumbled across this in my hunt for something
relating to this.
10 Reasons That People Are Hateful Of Motor Vehicle Law Motor vehicle accident attorney
These products are generally marketed as CBD products.
Here is my blog … sugar bear sleep gummies ingredients
However, very few studies center on CBD and sleep.
My page; Bookmarks
Feel your best with Joy Organics’ what are broad spectrum cbd gummies Softgels.
How To Survive Your Boss On Which Is Best For
Online Grocery Shopping Kitchen Storage Solution
The amount of organic cbd gummies without thc in each gummy depends on the
brand.
FOCL CBD + CBN Sleep Gummies have quite the résumé.
Look at my homepage – Bookmarks
The 10 Most Terrifying Things About Online Retailers Uk Stats online
retailers uk stats (Veronique)
can you get claritin without a prescription
Full-spectrum cbd gummies for sale at cvs contain trace amounts of THC.
Many reputable CBD brands sell purekana cbd gummies for sale near me
gummies online.
Windows Repairs Tips From The Top In The Business Near By
I’m not that much of a internet reader to be honest but your sites really nice,
keep it up! I’ll go ahead and bookmark your website to come back later on. All the best
Joy Organics = 11.75 points. Joy Organics = 21 points.
Here is my website: organic cbd gummies no thc