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
American Fridge Freezer Sale: 11 Things You’re
Not Doing smallest american fridge freezer
This Is The Myths And Facts Behind Car Diagnostic diagnostics Check
Stop by my blog … Poker Strategies
You’ll Never Guess This Cheap Fridge Freezers’s Benefits cheap fridge freezers (Dorine)
https://finasteride.store/# propecia cost
The Reason Why Adding A Double Glazed Windows Repairs To Your Life’s Activities Will Make All The Difference Window
Repairs (Fakenews.win)
15 Ideas For Gifts For Your Secondary Double Glazing Near Me Lover In Your Life Double Glazing Window Replacement
фулфилмент вайлдберриз https://24fulfilment-marketplace.ru/
Way cool! Some very valid points! I appreciate you penning this article and the rest of the site is also very
good.
17 Reasons Why You Shouldn’t Not Ignore Small Double Mattresses Bunk Bed Mattresses
A Provocative Remark About Online Shopping Top 7 5W-30 Conventional Motor Oil (Carson)
5 Killer Quora Answers To Mesothelioma Claim
Mesothelioma litigation
фулфилмент москва вайлдберриз https://24fulfilment-marketplace.ru/
Are You Tired Of Double Glazing Deals Near Me?
10 Inspirational Ideas To Bring Back Your Passion double glazing window handles
10-Pinterest Accounts You Should Follow About Bmw Replacement Key Cost bmw replacement Key cost uk
20 mg prednisone daily
Five Tools That Everyone Who Works In The Malpractice Legal Industry Should
Be Utilizing malpractice lawsuits
фулфилмент озон https://24fulfilment-marketplace.ru/
A fascinating discussion is worth comment. I do think
that you should publish more about this subject, it might not be a taboo subject but typically people do
not speak about such topics. To the next! Cheers!!
Paragraph writing is also a excitement, if you know afterward you can write if not it
is difficult to write.
10 Simple Ways To Figure Out Your Double Glazing
Companies Near Me repairs
Wow that was odd. I just wrote an extremely long comment
but after I clicked submit my comment didn’t appear.
Grrrr… well I’m not writing all that over again. Anyway, just wanted to say superb blog!
buy cytotec over the counter: buy cytotec – cytotec pills buy online
50 mg prednisone tablet
cost of valtrex generic
услуги фулфилмента в москве https://24fulfilment-marketplace.ru/
10 Best Facebook Pages Of All Time About Automotive Door Lock Repair Car lock Repairs near me
Will CSGO Case Prices Go Down Tools To Improve
Your Daily Lifethe One Will CSGO Case Prices Go Down Trick That Should Be Used By Everyone Be Able To Case glove
Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything.
I truly enjoy reading your blog and I look forward to your new updates.
lisinopril 240: lisinopril buy without prescription – lisinopril 10 mg tablet price