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
wonderful issues altogether, you just won a emblem new reader.
What could you recommend about your post that you just made some days in the past?
Any sure?
Wow, wonderful blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your site is magnificent, let alone the
content!
20 Trailblazers Setting The Standard In Double Glazing Repair Near Me double glazing near me (Ernestine)
I have realized some new points from your web site about computer systems. Another thing I’ve always considered is that computers have become a product that each family must have for many reasons. They provide convenient ways in which to organize homes, pay bills, shop, study, listen to music and perhaps watch tv programs. An innovative technique to complete most of these tasks is with a mobile computer. These desktops are mobile ones, small, highly effective and convenient.
Wow, this piece of witing is good, my sister is analyzing these things, thujs I am going to convey her.
Guide To 18 Wheeler Accident Attorneys: The Intermediate Guide For 18 Wheeler Accident Attorneys 18 Wheeler Accident Attorneys
Heya! I’m at work browsing your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts!
Keep up the excellent work!
Also visit my blog – Link Login Alt Dor77
Ten Stereotypes About Best American-Style Fridge Freezer That Don’t Always Hold zackfoxworth.top
can i get generic allopurinol without prescription
The 10 Most Terrifying Things About Online Shopping Uk Online Shopping uk
how can i get cheap cipro without a prescription
Five Killer Quora Answers To Veterans Disability Attorneys
Veterans disability attorney
buying cheap finpecia
is it ok to take magnesium with prasugrel
After exploring a number of the blog posts on your site,
I truly like your technique of writing a blog. I book marked it
to my bookmark website list and will be checking back soon. Please check out my website too and tell me your opinion.
15 Gifts For The Railroad Injuries Attorneys Lover In Your
Life railroad injuries lawsuits (http://dongnampack.co.kr/bbs/board.php?bo_table=free&wr_Id=235271)
Relief from pain and swelling in joints
Upvc Front Doors: The Good, The Bad, And The Ugly lock upvc door (Giuseppe)
Spot on with this write-up, I absolutely think this amazing site needs much more attention. I’ll probably be back again to see more,
thanks for the information!
Feel free to visit my web-site :: 777 Slot Online Gacor Dor77
Hi! can i get cheap finasteride for sale beneficial web page.
The Hidden Secrets Of Window Repair Near upvc window
repairs (Valerie)
7 Things About Personal Injury Attorneys You’ll Kick Yourself For Not Knowing personal injury lawsuit – dmonster506.dmonster.kr,
This website was… how do I say it? Relevant!!
Finally I have found something which helped me. Many thanks!
15 Startling Facts About Dangerous Drugs That You Never Knew Dangerous Drugs Lawsuits
whoah thiѕ blog is fangastic i rеally like reading your posts.
Keep up the great work! You realize, a lot оf individuals aгe searching aroսnd for tһiѕ info, yοu can aid them greatly.
cost of doxycycline online
How To Ship To Ireland From Uk Techniques To Simplify Your Everyday Lifethe Only How To Ship To Ireland From Uk Trick Every Person Should Learn how to ship to ireland from uk (Randy)
Window Repair Near Me Tools To Streamline Your Everyday Lifethe Only
Window Repair Near Me Trick That Should Be Used By Everyone Be Able To window repair near me
Hi i am kavin, its my first time to commenting anyplace, when i read this article i thought i could also create
comment due to this sensible paragraph.
I love what you guys tend to be up too. Such clever work and exposure!
Keep up the great works guys I’ve incorporated you guys to my blogroll.