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
Three Reasons To Identify Why Your Door Fitter Leeds Isn’t Working (And How To Fix It) leeds double glazing Repairs
Guide To Shop Online Uk Women’s Fashion: The Intermediate
Guide On Shop Online Uk Women’s Fashion shop online uk women’s fashion (m.ayoueryi.com)
п»їkamagra kamagra.win Kamagra Oral Jelly
hey there and thank you for your info on explainer Videos ? I have certainly picked up something new from right here. I did however expertise a few technical issues using this web site, as I experienced to reload the web site many times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading instances times will very frequently affect your placement in google and can damage your high quality score if advertising and marketing with Adwords. Anyway I am adding this RSS to my email and could look out for much more of your respective intriguing content. Ensure that you update this again soon.. https://local.gocommercially.com/business/explainer-video-company-india
https://cialist.pro/# Buy Cialis online
Kamagra 100mg price kamagra pills Kamagra tablets
What NOT To Do Within The Designer Handbags Red Industry
Designer bags
Nice post. I was checking continuously this explainer video blog and I am impressed! Very useful information particularly the last part 🙂 I care for such info a lot. I was seeking this particular information for a very long time. Thank you and best of luck. https://controlc.Com/54d90949
Cenforce 150 mg online: cenforce for sale – order cenforce
The 10 Scariest Things About Cerebral Palsy Attorneys cerebral palsy attorneys (Mickie)
What Is Double Bunk? And How To Use It double bunkbed (https://humanlove.stream/wiki/Rouseryan1773)
Where Are You Going To Find Smeg Fridge Freezers Be 1 Year From Today?
Best integrated fridge freezer 2022
Washer And Dryer In One Machine Explained In Fewer Than 140 Characters washer And dryer In one machine
Levitra online USA fast: levitrav.store – Buy Vardenafil 20mg
Are You Responsible For The Uk Online Grocery Shopping Sites
Budget? 12 Top Ways To Spend Your Money photography Lighting kit
I don’t know if it’s just me or if everybody else experiencing issues with your blog.
It appears as though some of the text within your posts
are running off the screen. Can someone else please comment and let
me know if this is happening to them too? This may be a problem with my web browser because
I’ve had this happen before. Kudos
It is explainer video company india perfect time to make some plans for the expaliner videos and it is time to be happy. I’ve read this post and if I could I wish to suggest you few interesting things or suggestions. Maybe you can write next articles referring to this article. I wish to read even more things about it! https://www.nextbizthing.com/business-support-20-supplies-20-services/explainer-video-company-india
Seven Reasons To Explain Why Cerebral Palsy
Lawyers Is Important cerebral palsy attorneys
Viagra online price Cheap Viagra 100mg Cheap Sildenafil 100mg
Titration ADHD Tools To Ease Your Daily Lifethe One Titration ADHD Trick Every Person Should Be Able To titration adhd
The Reason Glass Repair Leeds Is So Beneficial When COVID-19 Is In Session door repairs leeds – shorl.com –
my web-site :: craiglistforsex
sildenafil oral jelly 100mg kamagra: п»їkamagra – Kamagra 100mg price
9 Lessons Your Parents Teach You About Upvc Window Repairs Near Me window repairs Near me
Great post. I used to be checking continuously this blog and I’m inspired!
Very helpful info specially the ultimate part :
) I take care of such information much. I was seeking this particular information for a very long time.
Thank you and good luck.
Hi from explainer video company india, just became alert to your blog through Google Explainer video Company india, and found that it’s truly informative. I?m gonna watch out for brussels. I will appreciate if you continue this in future. Numerous people will be benefited from your writing. Cheers! https://www.Finditangeles.Com/services/explainer-video-company-india
Five Killer Quora Answers On Designer Handbags Uk Sale designer handbags uk Sale
https://cialist.pro/# Cialis 20mg price in USA
The 10 Most Terrifying Things About List Of Online Shopping
Sites Uk list of Online shopping sites uk
https://cenforce.pro/# cenforce.pro