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
This site was… how do you say it? Relevant!! Finally I have found something which helped me.
Thank you!
Cancer
Are you looking to enhance your online financial transactions?
Buy a verified Cash App account today and experience hassle-free payments.
With verified accounts, you can enjoy increased limits, added security, and peace of mind.
Don’t miss out on this convenient and reliable way to handle your money.
Get your verified Cash App account now!
%%
Check out my webpage; double glazing repair sutton (Guy)
%%
My web blog Ghost Immobiliser
Designer Handbags London
If you’re looking to buy a verified Cash App account, you’re in luck!
Verified accounts offer added security and trust, ensuring smooth transactions and peace of mind.
With a verified account, you can send and receive money without any worries.
So, don’t wait any longer, get yourself a verified Cash App account today!
http://www.videotrain.fr/
Hey there just wanted to give you a quick heads up and let you know a few of the
images aren’t loading properly. I’m not sure why but I think its a linking issue.
I’ve tried it in two different web browsers and both show
the same outcome.
asbestos lawyer Asbestos
Unlock a seamless digital payment experience with verified Cash App
accounts. Avoid limitations and enjoy unrestricted transactions.
Buy now and simplify your financial transactions today!
exposure
I am sure this paragraph has touched all the
internet visitors, its really really good paragraph on building up new
website.
%%
Also visit my web page; How To Work For Avon Online
%%
Take a look at my web site … Cbd Full Spectrum Uk
(http://Intranet.Welfarebox.Com)
upvc windows basingstoke (Shirleen)
%%
Here is my blog hemel hempstead electricians; Bookingsilo.Trade,
Hi to all, how is all, I think every one is getting more from this site,
and your views are fastidious designed for new viewers.
%%
Feel free to surf to my web blog: fridges for sale
We are a bunch of volunteers and opening a brand new scheme in our community.
Your website offered us with useful information to work
on. You have done a formidable activity and our whole community will be thankful to you.
can i claim Compensation for asbestos exposure
%%
Take a look at my webpage: Asbestos Lawsuit
I got this web page from my pal who shared with me concerning this web
page and now this time I am visiting this web site and reading very informative articles or reviews at this time.
asbestos Cancer Law lawyer mesothelioma settlement (https://french-meier-2.technetbloggers.de)
asbestosis settlement amounts (Dannielle)
best mesothelioma lawyer pay (Lizzie)
Mesothelioma lawyer Attorney
online Designer handbags (alfatv.Cz)
%%
Feel free to surf to my site: replacement windows redbridge (Reynaldo)
If you’re looking to buy a verified Cash App account, it’s important to do
your research and proceed with caution. While it may seem like a convenient
option, there are risks involved. Make sure to verify the credibility of
the seller, consider potential scams, and be aware that
it violates Cash App’s terms and conditions. It’s always safer to create your own account
or verify your existing one following the proper procedures.
Why You Should Concentrate On Enhancing Laundry Washer And Dryer
Combo
Take a look at my site :: washing Machines