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
I got this web page from my pal who told me concerning this site and now this time I am browsing this web site
and reading very informative posts here.
15 Gifts For The How Many Cases Are There In CSGO Lover In Your Life cs2 case (Lee)
8 Tips To Increase Your Online Shopping Sites Uk Game vimeo.com
Присоединяйтесь к группам объявлений России!
Частные объявления всегда были и будут бесплатными.
Соблюдайте правила групп, чтобы размещать Ваши объявления
Подписывайтся на свою группу, чтобы размещать объявления. https://t.me/addlist/2JY5f6-xD9Y4ODIy
Double Glazed Windows Repairs Tools To Streamline Your
Daily Life Replace
5 Killer Quora Answers On Car Accident Attorneys car accident attorneys (Marcus)
Guide To Mesothelioma Attorneys: The Intermediate Guide
To Mesothelioma Attorneys mesothelioma (http://web018.dmonster.kr/bbs/board.php?bo_table=b0601&wr_id=1594265)
http://ciprofloxacin.tech/# ciprofloxacin generic price
10 Websites To Help You Learn To Be An Expert In List Of Online Shopping Sites In Uk Zippo Pocket Lighter
The Little-Known Benefits Replace Bmw Key replacement key for bmw
Adhd In Adults Medication: What’s No One Is
Talking About add In adults medication
http://cytotec.club/# cytotec abortion pill
https://finasteride.store/# how cЙ‘n i get cheap propecia pills
Can I Buy From A Uk Website: Myths And Facts Behind Can I
Buy From A Uk Website Durable Car Battery Cable (Lolita)
20 Best Tweets Of All Time About Work From Home Jobs Online
Uk work from home jobs online
http://lisinopril.network/# can i buy lisinopril over the counter
What’s up colleagues, nice piece of writing and pleasant arguments commented at this place, I am
in fact enjoying by these.
What’s The Job Market For Double Glazed Repairs Near Me
Professionals? Local double glazing repair
Five Assessment Mental Health Projects For Any Budget assessing
mental health – Alexis –
17 Reasons You Shouldn’t Not Ignore Locksmiths Car
car key fob locksmith near me
What Fiat 500 Spare Key You’ll Use As Your
Next Big Obsession Fiat Remote Key
The No. One Question That Everyone In Double Bed Bunk Beds Should Be
Able To Answer 2 double bunk beds
online valtrex prescription
10 Mesothelioma Settlement Tips All Experts Recommend Asbestos attorney (http://www.plantsg.Com.Sg)
10 Freezer For Outbuilding Tricks Experts Recommend top freezer brands
My brother recommended I may like this website. He was once totally right.
This publish truly made my day. You can not
imagine just how much time I had spent for this info!
Thanks!
10 Factors To Know Concerning Built-In Fridge You Didn’t Learn In School fridge zanussi
A Look Into The Future What Is The Double Glazed Window Suppliers Near Me Industry Look Like In 10 Years?
repairs double glazed windows
ciprofloxacin order online buy ciprofloxacin ciprofloxacin mail online
You’ll Never Guess This Private Mental Health Assessment’s Tricks Private mental health assessment