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 goods from you explainer video company india, man. I’ve understand your stuff explainer video previous to and you are just extremely wonderful. I really like explainer video what you’ve acquired here, certainly like what you’re saying and the way in which you say it. You make it enjoyable and you still take care of to keep it wise. I can’t wait to read far more from you. This is actually a tremendous website. https://instantadz.Com/464/posts/3-services/27-other/1915110-explainer-video-company-india.Html
9 Lessons Your Parents Teach You About Car Key Repair Cost Car key repair cost
Cheapest Sildenafil online: Buy Viagra online cheap – sildenafil 50 mg price
7 Things You’ve Never Known About Become An Avon Rep Online becoming a avon representative
The Most Worst Nightmare Concerning Mens Anal Sex Toy It’s
Coming To Life Anal Toys Uk
Why Dangerous Drugs Attorneys Is A Lot More Dangerous Than You Believed Lower burrell dangerous drugs lawyer
cheapest cialis buy cialis online Cialis 20mg price
What Is The Reason Erb’s Palsy Lawsuit Is The Right Choice For You?
Vimeo
Excellent post. I was checking continuously this explainer video blog and I’m impressed! Very useful information specially the last part 🙂 I care for such info a lot. I was looking for this particular information for a long time. Thank you and best of luck. https://az-directory.com/listings12711100/explainer-video-company-india
buy Kamagra: buy kamagra online – Kamagra 100mg price
An Easy-To-Follow Guide To Bunk Bed For Sale bunk beds Stores
The 10 Most Terrifying Things About Car Seat Key car seat key, Penny,
http://viagras.online/# viagra canada
viagra without prescription: Cheap Viagra 100mg – buy Viagra online
9 Things Your Parents Teach You About Double Glazing
Window Repairs Near Me window Repairs near me
The 10 Most Scariest Things About Gotogel Link Alternatif Gotogel Link Alternatif
https://kamagra.win/# п»їkamagra
What Is Vauxhall Astra Key Replacement? History
Of Vauxhall Astra Key Replacement Program
The Comprehensive Guide To Boat Accident Claim boat accident lawsuits
kamagra: kamagra oral jelly – Kamagra 100mg price
hey there and thank you for your info on explainer Videos ? I have definitely picked up something new from right here. I did however expertise some technical points using this web site, as I experienced to reload the web site many times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and could damage your high-quality score if ads and marketing with Adwords. Well I am adding this RSS to my email and can look out for much more of your respective fascinating content. Make sure you update this again very soon.. https://en-web-directory.com/listings12705641/explainer-video-company-india
hello there and thank you for your information on explainer Videos ? I have certainly picked up anything new from right here. I did however expertise several technical issues using this web site, as I experienced to reload the site lots of times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I’m complaining, but slow loading instances times will often affect your placement in google and could damage your high quality score if ads and marketing with Adwords. Well I?m adding this RSS to my email and can look out for much more of your respective interesting content. Ensure that you update this again soon.. https://www.stridenation.com/users/Explainer_Video_Company_India
9 Things Your Parents Teach You About Prada Shoulder Bag
prada Shoulder bag
15 Gifts For The Link Login Gotogel Lover In Your Life online slot
9 Lessons Your Parents Teach You About Bmw Replacement Car Keys bmw replacement car keys
Excellent post. I was checking constantly this explainer video blog and I’m impressed! Extremely useful info specially the last part 🙂 I care for such info much. I was looking for this particular info for a very long time. Thank you and best of luck. http://prsync.Com/explainer-video-company-india/
The 10 Scariest Things About Online Grocery Stores That Ship Online grocery Stores that ship
“Ask Me Anything,” 10 Responses To Your Questions About Windows Leeds Upvc
http://kamagra.win/# super kamagra
The Hidden Secrets Of Treadmill Folded Up http://www.hometreadmills.uk