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
Excellent web site you have here.. It’s difficult to find high quality writing like yours nowadays.
I seriously appreciate people like you! Take care!!
Here is my web-site; Pinterest
I got this web site from my pal who told me about this site and at the moment this time I am browsing this web site and reading very informative posts at this place.
Всем привет! Может кто знает, где найти разные статьи о недвижимости? Сейчас читаю https://u-mechanik.ru
На Tubba RU https://tubba.ru доступны фильмы и сериалы с профессиональным дубляжом на русском языке.
Beat Economic Downturn Grinch With A Holiday Loan 대출 갈아타기
Medicament information sheet. Effects of Drug Abuse.
can i buy cheap propecia without insurance
Some information about pills. Get here.
End of 2030: By 2030, the https://smog-token.io/
can create large community based on a completely updated Solana ecosystem.
Good day! I know this is kinda off topic nevertheless I’d
figured I’d ask. Would you be interested in trading
links or maybe guest writing a blog article or vice-versa?
My website goes over a lot of the same subjects as yours and I feel we could greatly benefit from each other.
If you happen to be interested feel free to shoot me an e-mail.
I look forward to hearing from you! Wonderful blog
by the way!
Trey Poker Computer Card Game: Double The Amount Fun, Twice The Winnings 카카오 대출
Face Economic Independence Survey Crunch A Great All Purpose Personal Loan 신혼부부 대출
15 Shocking Facts About All SEO Company In London best seo company (Edwina)
Приветствую. Может кто знает, где почитать разные статьи о недвижимости? Сейчас читаю https://u-mechanik.ru
Do you mind if I quote a couple of your articles as long as I provide credit
and sources back to your site? My blog is in the very same niche as
yours and my visitors would truly benefit from some of the information you provide here.
Please let me know if this okay with you. Appreciate it!
Here’s An Interesting Fact About Medical Malpractice Lawyers.
Medical Malpractice Lawyers Medical malpractice lawsuit
Pills information sheet. What side effects?
where to get generic keflex without dr prescription
Some what you want to know about medication. Get information here.
Does your blog have a contact page? I’m having a tough time locating it but, I’d like
to send you an e-mail. I’ve got some creative ideas
for your blog you might be interested in hearing. Either way, great website
and I look forward to seeing it develop
over time.
Who Is Responsible For An Spare Keys Cut Budget?
12 Top Notch Ways To Spend Your Money 99811760
You have made some decent points there. I looked on the net for more info about the issue and found most people
will go along with your views on this website.
Приветствую. Подскажите, где найти разные статьи о недвижимости? Сейчас читаю https://u-mechanik.ru
The Reason Attorney For Asbestos Is So Beneficial For COVID-19 cassylawn.top
casino
Hi there! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I ended up
losing months of hard work due to no back up.
Do you have any methods to protect against hackers?
It’s going to be finish of mine day, except before ending I am reading this fantastic post to increase my experience.
Visit my blog post – phenq mechanism of action
Drug information sheet. Short-Term Effects.
cheap sinemet no prescription
Best trends of medication. Get information here.
The 10 Most Scariest Things About SEO Optimization Near Me seo optimization near me
Latest news on the Vinicius Junior fan site https://vinisius-junior.com. Vinicius Junior has been playing since 2018 for Real Madrid (Real Madrid). He plays in the Left Winger position.
Отличный сатй! Всем рекомендую!продать золото
7 Useful Tips For Making The Maximum Use Of Your American Fridgefreezer zackfoxworth
The Treadmills That Fold Flat Case Study You’ll Never Forget https://www.zackfoxworth.top/
Привет Друзья!
Всегда думал что купить диплом о высшем образовании это миф и нереально, но все оказалось не так, изначально искал информацию про: купить диплом в химках, купить диплом медицинского училища, купить новый диплом, купить диплом в салавате, купить диплом в минеральных водах, потом про дипломы вузов, подробнее здесь http://www.pigcraft.ugu.pl/member.php?action=profile&uid=234362
Оказалось все возможно, официально со специальными условия по упрощенным программам, так и сделал и теперь у меня есть диплом вуза Москвы нового образца, что советую и вам!
Хорошей учебы!