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
20 Insightful Quotes About Window Replacement window replacement panes
10 Facts About Www Avon Uk Com Representative Login That Can Instantly Put You In Good
Mood Www Avon uk Com representative Login
https://lisinopril.network/# lisinopril 5 mg tabs
Double Glazing Firms Near Me: What’s The Only Thing Nobody Is Talking About locks
lisinopril 10 mg 12.5mg buy lisinopril online india prinivil 20 mg tablet
15 Gifts For The Online Retailers Uk Stats Lover
In Your Life Vimeo
http://ciprofloxacin.tech/# buy cipro online canada
There have been two winners from Florida and one particular every in Texas, California, Connecticut and Maryland.
Here is my webpage :: https://pencis.com/the-excitement-of-finding-a-powerball-winning-ticket/
20 Questions You Should Always Ask About Saab Key Fob Programming Before You Buy Saab Key
Fob Programming saab 93 Key Fob
10 Things That Your Family Taught You About Adhd
Assessment For Adults adhd assessment For adults
Looking Into The Future How Will The Medical Malpractice Lawyer Industry Look Like In 10
Years? attorneys
Hey There. I found your blog using msn. This is a very
well written article. I’ll be sure to bookmark it and return to
read more of your useful info. Thanks for the post.
I will certainly comeback.
Why You Should Concentrate On Improving Repair Upvc Window upvc Window repair
10 Designer Handbags-Related Projects To Stretch
Your Creativity Designer Handbags Names
Why Adding A Personal Injury Lawyer To Your Life’s Routine Will Make The Difference
personal injury attorney, Shelton,
https://nolvadex.life/# tamoxifen vs raloxifene
10 Life Lessons We Can Learn From Fob Key Repair key repair Car
buy cipro online without prescription ciprofloxacin 500 mg tablet price buy ciprofloxacin over the counter
Great items from you, man. I’ve have in mind your stuff prior to and you’re just too wonderful.
I really like what you’ve got here, really like what you are stating and the way by which you assert it.
You’re making it enjoyable and you still take care of to stay it smart.
I cant wait to read much more from you. That is actually a terrific web site.
https://finasteride.store/# cost generic propecia without rx
Three Common Reasons Your Upvc Window Repairs Isn’t Working (And How
To Fix It) Upvc Window Repairs Near Me; Yerliakor.Com,
What’s The Job Market For Volvo Replacement Keys
Professionals Like? Volvo replacement keys
https://finasteride.store/# order generic propecia no prescription
5 Killer Quora Answers To Double Glazing Repairs Leeds double glazing Repairs leeds
See What Window Repair Leeds Tricks The Celebs Are Making Use Of window repair leeds (http://79Bo2.com)
tamoxifen dosage: what is tamoxifen used for – effexor and tamoxifen
zestoretic 20 12.5 lisinopril 20 mg prices lisinopril 3760
What Experts From The Field Want You To Know? repair car key fob
Why All The Fuss About Repair Misted Double Glazing Near
Me? double glazing window keys (Leah)
https://lisinopril.network/# lisinopril price uk