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
They have items to sell or solutions to offer.
my webpage; Bookmarks
Now That You’ve Purchased Coffee Machines Nespresso …
Now What? https://www.4182051.xyz/1ri15hw-u5qx1w-fe6t85l-59y1o-67t6xa-2542/
Asbestos Lawyer, a
naturally occurring mineral that is a natural mineral,
is used as an insulator and fire-resistant material.
Medicines information leaflet. Brand names.
can i buy minocycline for sale
Best what you want to know about pills. Get now.
NewULife Ranks. Welcome to my NewUlife Review.
My web page :: ai headshot generator reddit free
I probably modified the whole book writing services 10 various times.
Joy Organics’ organic cbd gummies for anxiety are also
vegan and non-GMO.
In this case, the life insurance firm’s money.
Also visit my site :: Bookmarks
We stand behind the high quality in our products.
Also visit my blog … is mushroom coffee safe
Wow, this paragraph is pleasant, my younger sister is
analyzing these things, so I am going to inform her.
They have products to sell or services to supply.
Here is my homepage … motion graphics for church free
can i buy pregabalin tablets
Have you ever thought about adding a little bit
more than just your articles? I mean, what you say is valuable and
everything. But think about if you added some great pictures or videos to give your posts more,
“pop”! Your content is excellent but with pics and videos, this site could definitely be one of the most beneficial in its niche.
Terrific blog!
Drug information for patients. Brand names.
get cheap coumadin without prescription
All what you want to know about medication. Read now.
Hi, yeah this piece of writing is genuinely fastidious and I have learned lot of things from it concerning
blogging. thanks.
The IQ is the very best completely dry natural herb vape I have attempted.
Here is my homepage: where can you buy vapes in australia
In this situation, the life insurance firm’s money.
Also visit my blog; ai headshot generator free reddit
I’m the owner operator of GOPARTYBUS party bus perth 20 seater.
Some people vape flavorful nicotine
Feel free to visit my page :: Bookmarks
9 Lessons Your Parents Taught You About Upvc Windows Near Me upvc Windows near me
How To Save Money On Sash Window Repair 257634.xyz
24 Hours For Improving Childrens Bunk Bed Edda Fay
The vapor and flavor high quality is likewise fantastic.
Feel free to surf to my homepage – Bookmarks
They have products to offer or solutions to provide.
my blog post: Bookmarks
Authentic HGH Perks (NewULife Hgh Gel Not Tested Yet).
My web-site … Bookmarks
Allow me reveal you in my evaluation of NewULife.
Feel free to surf to my page – ai headshot generator Free Best
whoah this blog is wonderful i really like studying your articles.
Keep up the great work! You realize, lots of people are looking round for this information, you could aid
them greatly.
Here is my website :: Versatile Men’s Hooded Tops
Organic Lion’s Mane review mushroom head air cushion cc cream Essence for $39.
5 Reasons To Be An Online Semi Truck Litigation Business And 5 Reasons Not To Semi Truck Accident Attorney
Some individuals plant based vape uk flavorful pure nicotine