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
Wow, awesome blog layout! How lengthy have you ever been running a blog
for? you make running a blog glance easy. The full look of your web site is magnificent, let
alone the content material!
Also visit my web site: new release dvd
https://vavada-kazi.ru/# вавада казино
buy viagra online
I just finished a bathroom renovation in my Chicago home Revive 360 bathroom remodeling
vavada: vavada-kazi.ru – казино вавада
mobile poker club скачать на андроид бесплатно
mobile poker club скачать на андроид на русском
https://vavada-kazi.ru/# вавада казино онлайн
you can to take this PDE5 inhibitor at a higher high dose approximately for half an hour before sexual contact so that no more than once per day.https://riskybusiness.org/
Купить диплом в России, предлагает наша компания
pinup пин ап вход пин ап казино
viktorova-ts.ru – Доверяйте профессионалам в создании кухни вашей мечты.
can i get cheap prednisone price
Кожен вік вимагає додому окремої https://www.5.ua/dv/info/316482?fbclid=IwAR2HO9IDHU6EoT4LsuI2jk-6h9_W1XCe3dD2kfec44zpWAnCECkKVrns3z8 увага.
pinup: pinup – пин ап казино онлайн
I really love your site.. Pleasant colors & theme.
Did you make this web site yourself? Please reply back as I’m wanting to create my very
own site and would like to know where you got this from
or exactly what the theme is called. Thank you!
пин ап зеркало: пин ап вход – pinup
Night Spa 알밤
I’ll right away snatch your rss feed as I can not in finding your email subscription hyperlink
or newsletter service. Do you have any? Kindly let me know
in order that I could subscribe. Thanks.
http://pinup-kazi.ru/# pinup
пин ап зеркало: пин ап зеркало – пин ап казино официальный сайт
Managing https://gitea.elatteria.com/tracicix196592 sugar levels is a critical aspect of maintaining overall health, especially for individuals with diabetes or those at risk of developing the condition.
dark web access tor markets links darknet sites
Hey! I know this is somewhat off topic but I was wondering if you knew where I could locate a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having trouble finding
one? Thanks a lot!
Feel free to surf to my web-site: specialized dementia care
Hello everybody, here every person is sharing these kinds
of familiarity, therefore it’s nice to read this blog, and I used to visit this weblog all
the time.
jackpoker скачать
jackpoker скачать онлайн
http://shth.ru/ — Подробности о дизайне и изготовлении кухонь.
Write more, thats all I have to say. Literally, it seems
as though you relied on the video to make your point.
You obviously know what youre talking about, why throw away your intelligence on just posting
videos to your blog when you could be giving us something enlightening to read?
Also visit my website; dvd distributors
can you buy generic valtrex without a prescription
This is really attention-grabbing, You’re a very skilled blogger.
I have joined your rss feed and look forward to in the hunt for more of your fantastic post.
Additionally, I’ve shared your website in my social networks
Have you ever considered creating an ebook or guest authoring on other websites?
I have a blog based on the same subjects you discuss and would love to have you share
some stories/information. I know my subscribers would appreciate your work.
If you’re even remotely interested, feel free to shoot me an email.