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
The Reasons To Focus On Enhancing Slot Gaming Experience Slot machine reviews
15 Things You’ve Never Known About Panels For Upvc Doors upvc doors
price, http://www.mhcf.Net,
Online Shopping Websites Clothes Techniques To Simplify
Your Everyday Lifethe Only Online Shopping Websites Clothes
Trick That Everyone Should Be Able To Online shopping websites Clothes
Are You Tired Of New Upvc Door? 10 Inspirational Sources To
Invigorate Your Love front upvc doors; Williams,
can you get cheap lexapro no prescription
Philippe Coutinho Correia https://philippecoutinho.prostoprosport-br.com Brazilian footballer, midfielder of the English club Aston Villa, playing on loan for the Qatari club Al-Duhail. He is known for his vision, passing, dribbling and long-range ability.
“Ask Me Anything,” 10 Responses To Your Questions About Veterans
Disability Attorney veterans disability lawsuits (Jestine)
The Top Reasons People Succeed In The How Many Cases Are
There In CSGO Industry Cs2 case
We stumbled over here from a different web page and thought I may as well check things out.
I like what I see so now i am following you. Look forward to exploring your web page again.
5 Common Myths About Truck Wreck Attorney You Should Stay
Clear Of truck accident (Shavonne)
1win казино промокод
10 Things That Your Family Teach You About Window Services London Window Services London
This Week’s Top Stories About Upvc Window Repairs Upvc Window Repairs Near Me
Lorem Ipsum has been the industry’s standard dummy text https://farmfactory.dk/bogxt of type and scrambled it to make a type specimen book.
5 Accident Lawyer Projects For Any Budget Accident Lawyers
cost cheap cordarone pill
20 Trailblazers Lead The Way In Slot Symbols video
slots (http://roll-express.ruwww.quilt-blog.de/serendipity/exit.php?url=ahr0cdovl3d3dy5zdgvzlnr5yy5lzhuudhcveg9vchmvbw9kdwxlcy9wcm9mawxll3vzzxjpbmzvlnbocd91awq9mtcymtizoa)
Erling Breut Haaland https://erling-haaland.prostoprosport-br.com Futebolista noruegues, atacante do clube ingles Manchester City e Selecao da Noruega. Detentor do recorde da Premier League inglesa em gols por temporada.
Why You Should Focus On Improving Play Slots online slot machines, http://www.valleyforge.Org,
7 Practical Tips For Making The Most Of Your Semi
Truck Compensation semi truck Accident Attorney
Hello there! can i order finasteride great internet site.
Keep on writing, great job!
buy tetracycline prices
Ahaa, its good conversation concerning this paragraph at this place at this website, I have read all that, so at this time me also commenting at this place.
A fascinating discussion is definitely worth comment.
I do think that you should publish more about this subject, it may not be a taboo subject but generally people do not speak
about these topics. To the next! Cheers!!
7 Simple Tricks To Rolling With Your Double Glazing Windows Repair Jere Alas
alternatives to prednisone for asthma
how to buy cipro without rx
Ten Adhd Symptoms Test Myths That Aren’t Always The Truth adhd Symptoms women adult
Keep this going please, great job!