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
Sale
%%
Review my web-site – Work From home jobs online
%%
my web site – upvc Door repairs redbridge (menwiki.men)
If you wish for to increase your know-how simply keep visiting this site and be updated with the newest gossip posted here.
%%
Here is my blog … asbestos legal
%%
Feel free to surf to my website; Headphone shop
I really like your blog.. very nice colors & theme.
Did you create this website yourself or did you hire
someone to do it for you? Plz answer back as I’m looking to construct my own blog and would like to find out where
u got this from. thanks a lot
%%
Here is my web site; Double glazing repair
A fascinating discussion is worth comment. I do believe that you need to write more on this topic, it might not be a taboo subject
but usually folks don’t talk about such issues.
To the next! Many thanks!!
asbestos lawsuit settlements
%%
My web page Porsche car key, Roche-decker.technetbloggers.de,
payout
Hello, I believe your blog might be having web browser compatibility
problems. When I take a look at your website in Safari, it looks fine but when opening in I.E., it has some overlapping issues.
I simply wanted to give you a quick heads up! Aside from
that, great website!
Buying a verified Cash App account can provide peace of mind and security for your financial
transactions. Verified accounts offer added benefits such as higher transaction limits and the
ability to withdraw funds instantly. With a verified Cash App account, you can easily and securely send and receive money, making it a valuable asset for anyone seeking a reliable and efficient digital payment solution.
%%
Also visit my web blog: asbestos compensation (https://www.ligra.cloud/)
%%
Feel free to surf to my website hounslow door and window
(https://grant-haney.hubstack.net)
Do you want to make hassle-free transactions with Cash
App? Buy a verified Cash App account today! With a verified account,
you can unlock increased transaction limits and gain additional security measures.
Experience seamless money transfers and enjoy the convenience
of a verified Cash App account. Don’t miss out on this opportunity, get yours now!
I couldn’t resist commenting. Very well written!
in wall electric fireplace (Nilda)
I am genuinely thankful to the holder of this web site who has shared this impressive post at at this time.
ghost immobiliser installation; Clemmie,
asbestosis settlement amounts (Doris)
designer handbags on sale Cheap
Your method of explaining everything in this piece of writing is actually good, every one be
able to simply understand it, Thanks a lot.
%%
Here is my webpage What is
Heya i am for the first time here. I came across this board and I find It really helpful & it helped me out a lot.
I’m hoping to give one thing again and aid others like you helped me.
ghost immobiliser installation (Rosemary)
%%
Review my homepage :: full spectrum cbd oil for
sale uk (Bettina)
Asbestos Lawsuit Attorney (Asbestoslawyers69506.Laowaiblog.Com)
asbestos compensation (highkeysocial.com)