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
How To Solve Issues With Upvc Replacement Door Handles upvc door Replacement lock
Why Real Money Slots Is A Must At The Very Least Once In Your Lifetime Online slot Machines
How To Design And Create Successful SEO Software For Small Business Tutorials On Home seo tools Google ranking
Москва Купить Мефедрон? Кристаллы МЕФ?
Где в Москве Купить Мефедрон? САЙТ – https://mephedrone.top/
Don’t Buy Into These “Trends” About Double Glazing
Repairs Cambridge cambridge windows and doors
20 Insightful Quotes About Slot Machine Games Scatter Slots
Купить Кокаин в Москве? Самый чистый Кокаин в Москве Купить
ССЫЛКА НА САЙТ- https://mephedrone.top
Five Killer Quora Answers On Standing Desk Treadmill Standing Desk Treadmill
Auto Accident Attorney 10 Things I’d Like To Have Known Earlier auto
accidents – Forum.Prolifeclinics.ro –
The Ultimate Glossary Of Terms About Fela Fela settlements
Medicines information sheet. Long-Term Effects.
generic lansoprazole for sale
Actual news about medicines. Read here.
5 Slot Machine Games Myths You Should Avoid Mobile-Friendly slots
Hey There. I found your weblog the usage of msn. This is an extremely
neatly written article. I’ll be sure to bookmark it and return to learn extra of your useful
info. Thank you for the post. I will certainly return.
Drug information sheet. Effects of Drug Abuse.
proscar generics
Some news about drug. Get information here.
See What Play Casino Slots Tricks The Celebs Are Using play casino Slots
These are genuinely great ideas in regarding blogging.
You have touched some nice factors here. Any way keep up wrinting.
What’s Holding Back The CS GO Cases To Open Industry? Shadow Case
Недавно решил я скачать интересное приложение mostbet зеркало на
свой андроид.
Also visit my page; https://mosttbetcl6.xyz/
15 Things You Didn’t Know About Window Repair Near Upvc Window Repairs
Five Killer Quora Answers On Slot Challenges Slot Challenges
Thank you, A good amount of facts.
great points altogether, you just received a emblem
new reader. What would you suggest about your submit that you made a few days ago?
Any certain?
10 Windows Repair Near Me Tricks Experts Recommend Double Glazed Window Repairs Near Me
אסיה היא היבשת ש”מספקת” את היפהפיות הסקסיות ביותר לעולם. עם זאת, הנשים שעובדות בשירותי העיסוי של דירות דיסקרטיות באשקלון מפתות, נושא התמחור. בבתי המלון אתם תמיד משלמים תעריף גבוה ועוד משלמים אותו עבור לילה שלם. דירות דיסקרטיות בקריות מאפשרות לכם לשלם לפי נערות ליווי מבוגרות
Many thanks! Useful stuff.
You’ll Be Unable To Guess Online Shopping Uk Sites’s Tricks online shopping
uk sites, Leatha,
10 Top Facebook Pages Of All Time About Modern Slots slot machine features; https://www.google.at/Url?q=https://rainbet.com/Casino/slots/relax-ignite-the-night,
Why Is This Dangerous Drugs Attorney So Beneficial?
When COVID-19 Is In Session dangerous drugs lawyer
10 Life Lessons We Can Learn From Online Shop Designer Suits Firearm Cleaning Kit (Vimeo.Com)
Nice answer back in return of this difficulty with real arguments and explaining the whole thing about that.