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
вывод из запоя ростов и область https://www.vyvod-iz-zapoya-rostov17.ru .
Patio Door Lock Repair Near Me Tools To Help You Manage Your Everyday Lifethe Only Patio Door Lock Repair Near Me Technique Every Person Needs To Learn patio door lock repair (Elke)
ozempic coupon buy ozempic pills online ozempic generic
Pretty nice post. I simply stumbled upon your weblog and wanted to mention that I’ve really enjoyed surfing around
your blog posts. After all I’ll be subscribing to your feed and I hope you write
once more soon!
Pill effects listed. Get medicine facts.
clarithromycin cz
Medication trends described. Find medication details.
rybelsus pill: rybelsus price – cheapest rybelsus pills
Свой Психолог сейчас
ozempic coupon: ozempic – buy ozempic pills online
В магазине сейфов предлагают где купить сейф купить сейф в москве цена
강북호빠 이번에 새식구를 모집중에 있습니다.
One Of The Most Innovative Things Happening With Locksmith Near Me For Car Local car Locksmiths
Казино Cryptoboss – Официальный сайт для криптовалютных игр
https://by-it.ru/
В магазине сейфов предлагают сейф купить цена можно купить сейф
What Is ADHD Treatment For Adults? History Of ADHD Treatment For Adults what Is adhd treatment for adults
https://rybelsus.shop/# buy semaglutide online
http://rybelsus.shop/# semaglutide online
Simply wish to say your article is as astonishing. The clarity on your publish is simply great and i can think you’re knowledgeable on this subject. Fine together with your permission let me to grab your RSS feed to stay updated with forthcoming post. Thanks a million and please keep up the enjoyable work.
vovan casino
https://rybelsus.shop/# rybelsus coupon
Are You Tired Of Realistic Sex Doll For Sale? 10 Inspirational Ideas To Bring Back Your Love Realisticsex Dolls
I like the valuable information you provide in your articles.
I will bookmark your weblog and check again here regularly.
I am quite certain I’ll learn plenty of new stuff right here!
Best of luck for the next!
Feel free to surf to my web page: emperor’s vigor tonic reviews
https://ozempic.art/# ozempic generic
15 Replacement Windows Benefits Everyone Needs To Be Able To upvc window lock replacement
This information is worth everyone’s attention. How can I find out more?
My website … torrent vpn
The Story Behind Cut Car Keys Can Haunt You Forever!Broken Car Key
buy semaglutide online rybelsus pill buy semaglutide online
Informative article, tottally what I wanted to
find.
My site قيمت ورق گالوانیزه
See What Freestanding Electric Fireplace Heater Tricks The Celebs Are Using freestanding electric fireplace (Rosie)
You’ll Never Guess This Mobility Scooter For Elderly’s Tricks Specialized Mobility Scooters
10 Top Facebook Pages That I’ve Ever Seen. ADHD Private Assesment Private adhd assessment North east
20 Interesting Quotes About Ultra Lightweight
Electric Wheelchair Mobility Electric chair (olderworkers.com.au)