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
Hey there! I know this is somewhat off topic but
I was wondering if you knew where I could find a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m having problems finding
one? Thanks a lot!
Link exchange is nothing else except it is only placing the other person’s blog link on your page at appropriate place and other person will also do same for you.
This Is The Ugly The Truth About Double Glazing Repair Kit window repairs, Charley,
Online Shopping Figures Uk Tools To Help You Manage Your Daily Life construction master pro-desktop 44080
Why Accident Lawyer Is Everywhere This Year Accident Attorneys
You’ll Never Guess This Online Clothes Shopping Websites Uk’s Secrets online
clothes shopping websites uk (Normand)
Guide To Car Key Repair Near Me: The Intermediate Guide
In Car Key Repair Near Me Car
8 Tips To Improve Your Private ADHD Assessment Game how to get a private adhd assessment
This article is in fact a good one it assists new internet viewers, who are wishing for blogging.
The People Nearest To Egg Vibrators Sex Toys Have Big Secrets To Share nearest
15 Double Glazed Window Repair Benefits That Everyone Should Know Window repairs near me
You’ll Never Guess This Coffee To Bean Machine’s Tricks coffee to bean machine
cytotec buy online usa: order cytotec online – cytotec pills buy online
best price for lisinopril
prednisone 7.5 mg
10 Apps To Aid You Control Your Uk Women’s Online Shopping Websites Vimeo.Com
http://ciprofloxacin.tech/# cipro online no prescription in the usa
http://finasteride.store/# cost of propecia without dr prescription
does tamoxifen cause weight loss: tamoxifen and osteoporosis – alternative to tamoxifen
http://nolvadex.life/# common side effects of tamoxifen
Your Worst Nightmare About Treatments For Adhd Come To Life Treating adult adhd
This Week’s Most Remarkable Stories About Best CSGO Opening Site
Best CSGO Opening Site counter-strike cases
8 Tips For Boosting Your Slot Technology Game slots with free bonus Rounds
What Is CS GO Cases To Open’ History? History Of CS GO Cases To Open case chroma
lisinopril tablets india: lisinopril 60 mg daily – can i order lisinopril online
What’s The Reason Everyone Is Talking About Amateur Slots Today Top-Rated Online Slots (Encoskr.Com)
You actually expressed that wonderfully!
Motorcycle Accident Lawyers Techniques To Simplify Your Everyday Lifethe Only Motorcycle Accident Lawyers Technique Every Person Needs To
Be Able To motorcycle accident Lawyers
The Three Greatest Moments In Mesothelioma Law History Mesothelioma litigation
10 Things That Your Family Taught You About London Window And
Door london window and door (Tawnya)