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
Allow me show you in my review of NewULife.
Also visit my web-site: functional mushroom gummies
Who Is Sash Double Glazing And Why You Should Be Concerned sash Window Replacement
Plant-based vapes have a host of prospective benefits.
Feel free to surf to my blog :: Bookmarks
https://ozempic.art/# ozempic cost
Respect to post author, some great information.
Also visit my webpage :: Bookmarks
Your average annual electricity consumption.
My web-site … Bookmarks
Wow! In the end I got a webpage from where I can in fact obtain valuable facts regarding my study and knowledge.
Also visit my page: reliable child
semaglutide tablets semaglutide online semaglutide tablets
It’s actually a great and helpful piece of info.
I’m glad that you shared this helpful info with us. Please keep us informed
like this. Thanks for sharing.
Here is my web page: Bookmarks
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make
your point. You clearly know what youre talking about, why throw away your
intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?
XYGENYX, a licensing business for FDA-registered products.
My web blog … euphoria mushroom gummies
Some people vape flavorful nicotine
Visit my homepage; where to buy plant based meals
compare energy prices australia
plans explained, with Canna Campbell.
Keep this going please, great job!
my page: Bookmarks
USP human growth hormone (somatropin).
My webpage functional Mushroom gummies
10 Best Mobile Apps For Shop Anal Toy women anal toys (https://dodd-may-2.Federatedjournals.com/)
The 12 Most Unpleasant Types Of Private ADHD Diagnosis UK The Twitter Accounts That You Follow adhd private Diagnosis Cost Uk
Solar energy systems come in all shapes and sizes.
My web-site: moving home electric ireland
I am sure this paragraph has touched all the internet viewers,
its really really nice paragraph on building up new
weblog.
My site; child care networks
They have products to sell or services to use.
My web-site … Bookmarks
Solar energy efficiency definition chemistry
systems come in all shapes and sizes.
Plant-based vapes have a host of prospective advantages.
my site :: Bookmarks
Hi there, I read your blog daily. Your story-telling style is awesome,
keep up the good work!
rybelsus cost rybelsus price cheapest rybelsus pills
https://rybelsus.shop/# buy semaglutide pills
What is New U Life SOMADERM Transdermal Gel.
my web-site … bookmarks
What Is Memory Foam Mattress Single And Why Is Everyone Dissing It?Small Single Mattress 2Ft 6
Unexpected Business Strategies For Business That Aided Wood Burning Stove Sale Succeed best wood burning stove uk; Agustin,
There are over 60 energy providers in Texas.
Also visit my web-site Bookmarks
Region also affects business compare energy plans with solar rates.