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
10 Best Facebook Pages Of All Time Concerning Add In Adult Women Inattentive Adhd In Women
I needed to thank you for this great read!! I absolutely enjoyed every little bit of it.
I’ve got you book marked to look at new stuff you post…
It’s Time To Expand Your Misted Up Glazing Repair Options Misty Double Glazing
10 Tell-Tale Warning Signs You Need To Get A New Best Squirting Dildos Strap on that Squirts
It is appropriate time to make a few plans for the long
run and it is time to be happy. I have learn this submit and if I may I wish to suggest you some interesting
issues or advice. Maybe you can write subsequent articles relating to
this article. I wish to read more issues about it!
В магазине сейфов предлагают новый сейф сейф купить в москве
What Freud Can Teach Us About Key Programing auto
вывод из запоя недорого ростов http://www.vyvod-iz-zapoya-rostov18.ru .
вызов нарколога на дом цена вызов нарколога на дом цена .
The Ugly Reality About Togel Singapore togel4D (https://atomcraft.Ru/)
Guide To Situs Togel Dan Slot Terpercaya: The Intermediate Guide Towards Situs Togel Dan Slot Terpercaya situs togel dan slot terpercaya
нарколог на дом цены нарколог на дом цены .
Excellent post. Keep writing such kind of info on your page.
Im really impressed by your site.
Hello there, You have done an incredible job.
I will definitely digg it and for my part recommend
to my friends. I’m sure they will be benefited from this website.
3 Ways That The Treadmill Sale UK Will Influence Your Life treadmills For sale; telegra.ph,
How To Make A Profitable Adhd In Females Symptoms Even If You’re Not Business-Savvy symptoms Of adhd in adults nhs (https://iampsychiatry70631.daneblogger.com/28469203/how-to-beat-your-boss-in-adhd-in-older-females-symptoms)
buy ozempic ozempic coupon buy ozempic pills online
вывод из запоя цены ростов на дону https://vyvod-iz-zapoya-rostov18.ru .
вызов нарколога на дом круглосуточно https://narkolog-na-dom-krasnodar15.ru/ .
Best Automatic Folding Mobility Scooter Uk Tools To Ease Your Daily Lifethe One Best Automatic Folding Mobility Scooter Uk Trick Every Individual Should Learn best automatic folding mobility Scooter Uk
Keep on writing, great job!
20 Up-Andcomers To Watch The Toto Result Industry 토토사이트
Best Automatic Folding Mobility Scooter Uk Tools To Help You Manage Your Everyday
Lifethe Only Best Automatic Folding Mobility Scooter Uk Trick That Should Be Used By Everyone Learn best automatic Folding Mobility scooter
анонимный вывод из запоя ростов анонимный вывод из запоя ростов .
Great info. Lucky me I ran across your blog by accident (stumbleupon).
I have bookmarked it for later!
My website – lottery defeater software app
You’ll Never Guess This Upvc Windows Birmingham’s Secrets Pvc Windows Birmingham [Willysforsale.Com]
вывод из запоя цены ростов-на-дону http://vyvod-iz-zapoya-rostov18.ru .
A fascinating discussion is definitely worth comment.
I think that you need to write more about this topic, it might not be a taboo subject
but generally people do not discuss such issues. To the next!
Kind regards!!
A Peek Inside Psychiatric Assessment Private’s Secrets Of Psychiatric Assessment Private how to Get psychiatric assessment
ozempic cost: ozempic – ozempic online
Nine Things That Your Parent Taught You About Double Glazing Repairs Near Me Double glazing repairs