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
%%
My web site volvo v70 key, Penny,
blood cancer caused by railroad how to get a settlement
%%
Visit my homepage :: diagnostic assessment mental health (Mittie)
I’d like to find out more? I’d love to find out some additional information.
double glazing repair basingstoke (Celsa)
%%
My web-site: Jon zherka Height
I have read so many articles about the blogger lovers except
this article is actually a fastidious post,
keep it up.
Here is my page: cosplay costumes women
asbestos cancer lawyer mesothelioma (https://lawyersforasbestoscases98431.isblog.net/the-Complete-guide-To-asbestos-cancer-law-lawyer-mesothelioma-38909175)
%%
My site … Psychiatry-Uk (420.Bio)
Hello there! This is my first visit to your blog!
We are a team of volunteers and starting a new initiative in a
community in the same niche. Your blog provided us
valuable information to work on. You have done a outstanding job!
%%
my web-site – electricians in Biggleswade (https://preston-lyons-2.blogbright.net)
%%
Also visit my website :: nearest
Buy Vardenafil 20mg online: Generic Levitra 20mg – Buy Vardenafil 20mg
Good article. I definitely love this website. Stick with
it!
Hello, I log on to your new stuff on a regular basis.
Your humoristic style is awesome, keep doing what
you’re doing!
Having read this I thought it was extremely enlightening.
I appreciate you spending some time and energy to put this informative article together.
I once again find myself personally spending a significant amount of time both reading and commenting.
But so what, it was still worth it!
If you’re looking to securely manage your transactions, it’s essential to buy a verified
Cash App account. A verified account ensures credibility and trustworthiness,
providing peace of mind when making payments or receiving funds.
With a verified account, you can confidently transact without
worrying about potential issues. Don’t compromise on security, invest in a verified
Cash App account today!
%%
Visit my web-site: hounslow door panels (https://te.legra.ph/Everything-You-Need-To-Learn-About-Upvc-Windows-Hounslow-10-01)
%%
my website – program
%%
Feel free to visit my blog: vauxhall insignia key fob; Layla,
%%
my website – Analyse a quote
%%
Also visit my blog :: become A rep
Nicely put, Appreciate it.
Feel free to surf to my site … https://gayblackboysblog.com
%%
Also visit my web blog :: repair to double glazed windows (Anton)
%%
Review my homepage: renault clio key replacement
talcum powder Mesothelioma lawsuit
%%
Also visit my site; double glazed window york – https://meyers-mathiesen.technetbloggers.de/7-simple-changes-that-will-make-the-difference-with-your-upvc-doors-york/,
%%
Also visit my web site :: suzuki key replacement uk – http://www.wy2bp2kp1dwvfq1j.com
–
%%
Here is my site; premium broad-spectrum cbd uk
Hello to every body, it’s my first visit of this weblog; this
weblog carries remarkable and really excellent stuff for visitors.