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
Drugs information sheet. Brand names.
how can i get generic minocycline no prescription
Everything trends of medication. Read here.
11 Strategies To Completely Block Your Mesothelioma Lawyer mesothelioma lawyers (Tyrone)
A lawsuit can be filed against any party whose Asbestos Attorney products caused harm.
After many years of litigation in the asbestos industry companies have gone bankrupt and
set up asbestos bankruptcy trustees.
In this case, the life insurance policy business’s cash.
Take a look at my web site … what is data decay
This vaporizer has a number of distinct attributes.
Also visit my page: where to buy Plant based meals
10 Things That Your Family Teach You About Online Famous Shopping Sites online famous shopping sites
The vapor and flavor high quality is additionally wonderful.
Also visit my website plant based vape
Medicine information for patients. Drug Class.
how can i get risperdal without rx
Everything what you want to know about meds. Get information now.
Have you ever considered about adding a little bit more than just your articles?
I mean, what you say is valuable and all. But just imagine if you added some great pictures or video clips to give your posts more, “pop”!
Your content is excellent but with images and videos, this
blog could undeniably be one of the most beneficial in its field.
Excellent blog!
Saved as a favorite, I like your website!
can you get generic tetracycline pills
Both mushrooms products are immune contractors.
Feel free to surf to my blog post :: best lions mane brand on amazon
This Is A Repair Double Glazing Window Success Story You’ll Never
Remember double glazed window repairs near me
Medicines prescribing information. What side effects?
can i purchase cheap divalproex without insurance
All trends of drugs. Read now.
The vapor and taste top quality is likewise excellent.
Look at my web page – Bookmarks
Five Killer Quora Answers To Cheap Online Electronics Shopping Uk cheap online electronics shopping Uk
can you buy generic cetirizine online
buying furosemide price
We screen, test, and monitor our editors.
Also visit my web-site … book editing jobs australia
Pills prescribing information. Cautions.
buy seroquel without insurance
Actual news about medicines. Read information here.
NewULife Ranks. Welcome to my NewUlife Testimonial.
Feel free to surf to my web-site – Bookmarks
Medication information sheet. Cautions.
generic colchicine
Some information about meds. Get information here.
prograf editora grfica ltda
Portable vaporizers make use of batteries as a power source.
Also visit my web site: Bookmarks
you’re actually a excellent webmaster. The web site loading pace
is amazing. It kind of feels that you’re
doing any distinctive trick. Moreover, The contents are masterwork.
you’ve done a fantastic process on this subject!
XYGENYX, a licensing company for FDA-registered products.
my web page Bookmarks
Medicament information sheet. Drug Class.
buy cheap prozac no prescription
Actual what you want to know about drug. Read information now.
20 Fun Facts About Double Glazing Misting Repair jerealas
They have items to market or solutions to
supply.
Feel free to visit my page: Best ai picture generator reddit
The Advanced Guide To Semi Truck Case accidents