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
The vapor and flavor top quality is likewise excellent.
Also visit my web blog – where to buy plant based meals
NewULife Ranks. Welcome to my NewUlife Review.
Have a look at my homepage; types of disability benefits uk
This vaporizer has a couple of unique attributes.
Also visit my blog post: Plant based vape near me
Discovering a language does not have to be costly.
my web-site – Bookmarks
Allow me show you in my review of NewULife.
My homepage – Bookmarks
You do NOT intend to consume Lion’s Mane Mushrooms raw.
Stop by my web page … is mushroom coffee Safe
5 Killer Quora Answers On Uk Online Shoe Shopping
Websites Uk online shoe shopping websites
Their production exclusively uses organic hemp.
my web page; where can i buy penguin cbd full spectrum gummies
What is New U Life SOMADERM Transdermal Gel.
Have a look at my web blog – Bookmarks
Medicines information sheet. Drug Class.
celebrex drug
Best what you want to know about drugs. Read here.
Both mushrooms products are immune building
contractors.
Here is my blog post … mellow mushroom gatlinburg reviews
15 Reasons Not To Ignore Folding Treadmill Zack Foxworth
Fantastic beat ! I wish to apprentice at the same time as
you amend your web site, how can i subscribe for a weblog web site?
The account helped me a appropriate deal. I have been tiny bit familiar of this your broadcast offered
vivid clear idea.
Feel free to surf to my homepage: best seo services in jalandhar
What is New U Life SOMADERM Transdermal Gel.
Here is my web-site … Bookmarks
Proofing is the very last phase in modifying a publication.
Visit my blog :: book writing services
Experienced and specialist motorists.
Feel free to surf to my page … party bus perth prices
In this instance, the life insurance company’s cash.
Feel free to surf to my web page; what causes rotting food
Medicine information leaflet. Generic Name.
can i get cozaar no prescription
Best information about meds. Read information here.
The IQ is the most effective completely dry natural
herb vape I have actually tried.
Stop by my blog canadian plant based meat companies
USP human growth hormonal agent (somatropin).
My web blog: Bookmarks
where can i get zyloprim without a prescription
Allow me reveal you in my review of NewULife.
Here is my homepage … Bookmarks
Medicines information. Long-Term Effects.
can i purchase cheap tamoxifen without a prescription
Everything what you want to know about medicament. Read here.
XYGENYX, a licensing company for FDA-registered items.
Also visit my webpage :: Bookmarks
In this situation, the life insurance policy company’s
loan.
Look at my website ai picture generator reddit
Someone essentially lend a hand to make critically articles I might state.
This is the first time I frequented your website page and thus far?
I surprised with the analysis you made to create this particular publish
amazing. Magnificent job!
Stop by my blog zib digital – best seo company in ahmedabad photos
10 Healthy Wall Mount Fireplace Habits lynnbolvin.top
Lorem Ipsum has been the industry’s standard dummy text https://cmedia-design.dk/jotjnom of type and scrambled it to make a type specimen book.
Everything is very open with a precise description of the challenges.
It was definitely informative. Your website is very helpful.
Thanks for sharing!
Not all proper cbd gummies for sale near me products are made with
the same quality.