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 site is https://www.igfitalia.org/. feel free to visit. it is all about mege888 megi888 mego888 megu888.my site not a scam. my site really not a scam. my site confirm not a scam.
%%
Feel free to surf to my web page … program
Thank you, I value this!
Hmm is anyone else having problems with the images on this blog loading?
I’m trying to find out if its a problem on my end or if
it’s the blog. Any feedback would be greatly appreciated.
Looking to buy a verified Cash App account? Look no further!
A verified Cash App account provides added security and peace of mind for your financial transactions.
Don’t compromise on safety, invest in a verified account today!
What’s up Dear, are you genuinely visiting this web page on a regular basis, if
so then you will without doubt obtain nice knowledge.
%%
my blog :: replacement key for saab 93 (Cyrus)
Looking for a hassle-free way to buy a verified Cash App account?
Look no further! With a verified account, you can enjoy enhanced security, increased transaction limits, and added convenience.
Don’t waste time, get your verified Cash App account today and start
enjoying seamless money transfers!
Very energetic blog, I loved that bit. Will there be
a part 2?
https://accounting11.b-cdn.net/research/accounting11-(123).html
Stick to a small yet stately earring and a cocktail ring, and hold extra sparkle to a minimal.
Looking to buy a verified Cash App account? Look no further!
Purchasing a verified account ensures safe and secure transactions, higher spending limits,
and access to various features. Save time and
effort by getting a verified Cash App account today! Enhance
your financial convenience with a trustworthy and reliable platform.
Don’t miss out on this opportunity.
It’s an remarkable article in favor of all the web viewers; they will obtain benefit from it I am sure.
Levitra online USA fast п»їLevitra price Cheap Levitra online
Buying a verified Cash App account offers numerous advantages.
With a verified account, you can enjoy increased transaction limits, better
security features, and access to additional functionalities.
It also provides peace of mind as verification implies a thorough background check.
So, if you want to make the most of your Cash App experience, consider investing in a verified account
today.
%%
Check out my blog; renault trafic key replacement; Leslee,
doğum haritası hesaplama,Nisan 2022 Burç Yorumları
%%
My site Subaru car key (http://dorette-Deutsch.de)
If you’re looking to buy a verified Cash App account, you’ve come to the right place.
A verified Cash App account offers added security and more features, making it a wise investment.
Don’t risk your money and personal information with unverified accounts –
get a credible one today!
Hi colleagues, its enormous article on the topic of tutoringand fully defined, keep it up all the time.
%%
my page: volvo v70 key (Dann)
Howdy! Do you know if they make any plugins to protect against hackers?
I’m kinda paranoid about losing everything I’ve worked hard on. Any tips?
%%
my web blog – Hot Amigo Order
%%
Also visit my homepage: bmw replacement keys; Beatrice,
хогвартс смотреть онлайн
Designer Handbags Black
what is the average settlement for asbestos claim – https://howlongdoesanasbestosclai08260.theisblog.com/,
%%
My web page :: asbestos mesothelioma claim (comedycoup.com)
asbestosis asbestos mesothelioma attorney (Katlyn)
Hi there, I log on to your blog like every week.
Your writing style is awesome, keep doing what you’re doing!
11 “Faux Pas” That Are Actually Acceptable To Create With Your wasatch
Railroad contractors lawsuit (sanford-Wilder.hubstack.net) Lawsuit
Mds