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
14 Questions You’re Afraid To Ask About Online Shopping Sites Uk Manual Control Gas Fireplace Logs
Hello, I desire to subscribe for this weblog to get hottest
updates, so where can i do it please help out.
Also visit my website King Size Ottoman Bed
Pills information leaflet. Generic Name.
buying cheap ipratropium no prescription
All about medicine. Get here.
20 Things Only The Most Devoted London Online Clothing Shopping Sites Fans Understand vimeo.Com
Truck Accident Lawyer: The History Of Truck Accident
Lawyer In 10 Milestones truck accident law firms – Reece
–
Usually I don’t read article on blogs, but I would like
to say that this write-up very forced me to take a look at and do it!
Your writing taste has been surprised me. Thank you, very nice article.
The 10 Most Scariest Things About Progressive Slots progressive
slots (http://www.google.com.ai)
Thanks, Ample content.
Also visit my webpage – find sex near me
20 Things You Must Be Educated About Online Shopping Top
7 vimeo.Com
Five Things You’re Not Sure About About New Slots Online casino Slot Machines
An In-Depth Look Back The Conversations People Had About Online
Slots 20 Years Ago hacksaw Casino slots online
Why Seat Key Fob Replacement Is More Tougher Than You Imagine
Seat Ibiza Key Programming
Discover Zeki Frucht GmbH: Your Trusted B2B Partner
Who We Are
Zeki Frucht GmbH, based in Vienna, Austria, has been a global player in the
wholesale industry for over 7 years. We specialize in providing premium soft drinks, beverages, and frozen foods to businesses worldwide.
Our commitment to quality, reliability, and top-tier
service sets us apart as a trusted partner for bulk supplies.
15 Top Slots For Fun Bloggers You Should Follow Top Slots
9 Lessons Your Parents Teach You About Parent Facing Pushchair parent Facing Pushchair
After looking into a few of the blog articles on your website, I honestly appreciate
your way of writing a blog. I saved it to my bookmark webpage list and will be checking back soon.
Please visit my website too and tell me what you think. https://www.Centrodentalmendoza.com/question/souliers-orthopediques-pourquoi-et-comment-les-choisir-3/
20 Things You Need To Be Educated About Best Slot Payouts best Online slots
5 Killer Quora Questions On Lightweight Double Buggy Compact Twin Stroller
Find Out More About Slot Tournaments While Working From Your Home slots with bonuses; hong-duk.com,
What’s The Current Job Market For Counter Strike Global Offensive Best Crosshair
Professionals? strike Global Offensive
5 Killer Quora Answers On Demo Gatotkaca Pragmatic gatotkaca pragmatic (Francisca)
Medicine information. Brand names.
where to get promethazine pill
Best information about medicines. Get here.
15 Top Pinterest Boards From All Time About Double Glazing Repair Near Me double glazing near me
What’s The Current Job Market For Truck Accident Attorney
Professionals? Truck Accident (X3.Wiki)
9 Signs That You’re An Expert Best Slot Payouts
Expert slots with bonuses [http://www.google.com.Uy]
“Врумврум.рф” – сайт чтобы разыскивания также продажи автомобилей, на коем пользователи смогут отыскать свежеиспеченные и бувшие в употреблении автомашины через разных продавцов. Фотосайт также выдает информацию что касается ценах, свойствах и отзывах что касается различных марках а также модификациях автомобилей.
Veterans Disability Lawyers Tools To Ease Your
Daily Lifethe One Veterans Disability Lawyers Trick Every Individual Should Be
Able To veterans Disability lawyers
What Freud Can Teach Us About Online Shop Bird Bath Solar Pump
10 Things You Learned In Kindergarden That Will Aid
You In Obtaining Twin Pushchair Twin Pushchairs