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://cytotec.club/# cytotec online
Who Is The World’s Top Expert On Online Shop Designer Suits?
low speed high torque Motor
http://nolvadex.life/# is nolvadex legal
cost of propecia for sale order cheap propecia prices cost of propecia tablets
What’s The Current Job Market For Tier 2 Seo Professionals?
tier 2 Backlinks
Watch Out: How Central Locking Repairs Near Me Is Taking Over And What We Can Do About It car Lock replacement
8 Tips To Up Your 3 Wheel Scooter For Handicapped
Game 3 Wheel Electric Mobility Scooter For Adults Uk
See What Replacement Sash Windows Tricks The Celebs Are Using replacement sash windows
https://finasteride.store/# order generic propecia without insurance
https://lisinopril.network/# lisinopril 10 mg tablet
lisinopril 10 mg canada: cost of lisinopril 40 mg – lisinopril 40 mg india
15 Up-And-Coming Work From Anywhere In The World Online Bloggers You Need To Check Out Part time Work from home
5 Double Glazing Offers Near Me Projects For Any Budget double Glazing replacement Window
(ezproxy.cityu.Edu.hk)
papaver somniferum семена почтой
I think this is among the most significant information for me.
And i’m glad reading your article. But wanna remark on few general things, The site style is ideal,
the articles is really excellent : D. Good job, cheers
Thanks for sharing your thoughts about Akcyza usa.
Regards
Five Killer Quora Answers On Locksmith Car Keys
locksmith car keys
You’ll Never Be Able To Figure Out This Window Replacement Near Me’s Tricks Window Replacement Near Me
The Secret Secrets Of Boat Accident Lawyers boat Accident lawsuits
I loved as much as you will receive carried out right here.
The sketch is attractive, your authored material
stylish. nonetheless, you command get got an shakiness over that you
wish be delivering the following. unwell unquestionably
come more formerly again as exactly the same nearly very often inside
case you shield this hike.
Quaⅼіty content is the secrеt to attract the viewers to pay a qhicқ visit
the web page, that’s what thіs web site is
providing.
my web blog – nirwana88
9 Things Your Parents Teach You About Mini Key Fobs mini key Fob
buy generic ciprofloxacin buy cipro cheap cipro ciprofloxacin
I absolutely love your blog.. Excellent colors & theme.
Did you make this site yourself? Please reply back as I’m wanting to create my own website and
would like to learn where you got this from or exactly what the theme is
named. Many thanks!
ciprofloxacin over the counter buy cipro online canada buy cipro online
Avon Book: The Good, The Bad, And The Ugly avon book july
(http://www.encoskr.com/bbs/bbs/board.php?bo_table=free&wr_id=1272409)
How To Become A Prosperous Accident Lawyer Even If You’re Not Business-Savvy accident Lawsuit
prednisone 20 mg without prescription
The People Nearest To Ecommerce SEO Agency Tell You Some Big Secrets nearest
15 Door Repairs Near Me Benefits That Everyone Should Be Able To upvc door repairs near me
[Annett]