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
https://andywwrj16161.blog-eye.com/27667733/pin-up-casino-and-mostbet-your-gateway-to-thrilling-enjoyment-and-lucrative-betting
Seu cassino da sorte espera por voce Blaze
If some one desires expert view about blogging and site-building afterward i
suggest him/her to pay a quick visit this blog, Keep up the pleasant work.
3 Common Causes For Why Your Online Shopping Sites In Uk For Electronics Isn’t Performing (And What You Can Do To Fix It) stainless steel hand blender
The People Closest To Online Shopping Uk Amazon Share Some Big Secrets Poster Frame 15X33
How To Outsmart Your Boss In Upvc Windows Repairs Repairing Upvc Windows
The Reasons Examples Of Online Products Is The Most Popular Topic In 2023 Sterilite With Plum Handles (Morgan)
You’ll Be Unable To Guess Federal Employers’s Benefits federal employers
Hello to every one, it’s truly a pleasant for me to visit this web site,
it consists of priceless Information.
Guide To Slot Wins: The Intermediate Guide Towards Slot Wins Slot Wins
Hey There. I found your weblog the usage of msn. That is a really well written article.
I will make sure to bookmark it and return to read more of your helpful information. Thank you for the post.
I’ll certainly comeback.
I don’t know whether it’s just me or if everybody else
experiencing problems with your site. It appears as though some of the written text
within your posts are running off the screen. Can somebody
else please comment and let me know if this is happening to them as well?
This may be a problem with my web browser because I’ve had this happen before.
Thanks
Also visit my web blog :: toddler zip towel
12 Companies Are Leading The Way In Slot Variance Exciting Casino Slots
WOW just what I was searching for. Came here by searching
for tote bag hush puppies
If you desire to obtain a good deal from this paragraph then you have to
apply such strategies to your won web site.
Also visit my web-site: Storage Bed
5 Must-Know Slot Mechanics-Practices You Need To Know For 2023 evoplay slots reels (http://Www.Asystechnik.com)
You’ll Never Guess This Shopping Online Uk To Ireland’s Tricks shopping Online uk to Ireland
9 Lessons Your Parents Teach You About Best Online Shopping Sites London best online shopping sites london (Chassidy)
One Key Trick Everybody Should Know The One Slot Gaming
Trick Every Person Should Know video slots (Blanche)
This Week’s Top Stories About Unique Slots Unique Slots High-Quality
Slots; https://Www.Cheaperseeker.Com/,
See What Demo Slot Zeus Rupiah Tricks The Celebs Are Making Use
Of Demo Slot Zeus Rupiah
Slot Promotions It’s Not As Expensive As You Think exciting slots (http://www.clean-ace8.com/bbs/board.php?bo_table=free&wr_id=350675)
A Comprehensive Guide To Mercedes Key Programmer.
Ultimate Guide To Mercedes Key Programmer mercedes
a class key (Lynda)
5 Killer Quora Answers On Best Slot Payouts slot payouts
Is Does Amazon Ship To Uk The Best Thing There Ever Was? Cooking Knife Set For Beginners
The Reason Slot Developers Is Fast Becoming The Most Popular
Trend In 2023 evoplay slots reels (Jacklyn)
שלהם. כאן הגבר יכול ליהנות מזמן איכות שבו הוא נמצא במרכז העניינים. זהו המקום בו אתה יכול לשכוח מהעבודה ומכל הצרות של החיים, מוכנות להגשים את משאלותיכם! אל תדאגו יותר מדי כאשר אתם מסתכלים על אישה. הסתכלו על הרגע הזה, מצד שני – זה לא בגידה, אלא ניסיון בחורות סקס
Very rapidly this web page will be famous amid
all blogging and site-building visitors, due to it’s pleasant posts
10 Things You’ll Need To Learn About Popular
Slots Scatter Slots (http://Bbs.Qupu123.Com)
The Time Has Come To Expand Your Princess Demo Options gates of princess demo slot