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
Very good knowledge Cheers!
The 9 Things Your Parents Teach You About Best 2 In 1 Prams 2 in 1 Prams
Hi there! Do you know if they make any plugins to assist with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing
very good gains. If you know of any please share. Thanks!
This is a topic which is close to my heart… Best wishes!
Exactly where are your contact details though?
Five Things You Didn’t Know About Window Replacement Near Me
glass replacement on windows
Hi there excellent website! Does running a blog such as this require a lot of work?
I’ve very little knowledge of coding but I
was hoping to start my own blog soon. Anyhow, should you have
any suggestions or techniques for new blog owners please share.
I know this is off subject however I just had to ask.
Thanks!
Five Killer Quora Answers On Replacement Windows South London Replacement windows South london
How Do I Explain Online Shopping Sites In Uk For Electronics To A Five-Year-Old Autosaver88 Led Light Bar 32 Inch – https://Vimeo.Com,
WOW just what I was looking for. Came here by searching for mesin slot gacor
Nine Things That Your Parent Teach You About Repair Patio Door
Lock repair patio door lock (Jennifer)
Have you ever considered publishing an ebook or guest
authoring on other websites? I have a blog based on the same topics you
discuss and would really like to have you share some stories/information. I know my subscribers would value your
work. If you’re even remotely interested, feel free to shoot me an e-mail.
Five Killer Quora Answers On Free Casino Slots Free Casino Slots
sexs-foto.com
купить диплом инженера купить диплом инженера .
You’ll Never Guess This Window Repair Near Me’s Tricks window repair near me
Five Killer Quora Answers To Window Frame Repair London window Frame repair london
A Peek Into The Secrets Of Upvc Door Panel Replacement replacement upvc door lock (Clark)
11 Ways To Completely Revamp Your Online Shopping non-fading acrylic inks
I always used to study article in news papers but now
as I am a user of internet so from now I am using net for articles, thanks
to web.
5 Killer Quora Answers On Best Online Shopping Websites Uk best online shopping websites uk
(Rebbeca)
מהעבודה! פינוק מלא תשוקה ללא מחויבות אם אתם רוצים לבלות זמן עם נערת עיסויים מלאת תשוקה שמחכה לכם במקומות כמו דירות דיסקרטיות בחדר מלון, או להגיע למקום דיסקרטי בו הבחורה חיה ועובדת. הזמנת דייט עם מי שאתם אוהבים היא קלה ופשוטה: התקשרו למספר הטלפון של נערות ליווי ברחובות
This Story Behind SEO Tools Software Will Haunt You Forever!
Buy Seo Software (http://Www.Cddc.Co.Kr)
you are truly a just right webmaster. The website loading velocity is amazing.
It kind of feels that you’re doing any unique trick.
In addition, The contents are masterpiece.
you have performed a excellent activity on this matter!
15 Reasons Not To Be Ignoring Best Online Shopping Websites Uk cat Food Airtight container
Woah! I’m really enjoying the template/theme of this website.
It’s simple, yet effective. A lot of times it’s very difficult to get
that “perfect balance” between user friendliness and appearance.
I must say you’ve done a amazing job with this. Also, the blog loads extremely quick for me
on Opera. Superb Blog!
5 Common Myths About Online Shop You Should Avoid Elegant Silver Bathroom Mirror
See What Amazon Uk Online Shopping Clothes Tricks The Celebs Are Using amazon uk online shopping clothes (https://Www.kfuc.co.kr/bbs/board.php?bo_table=free&wr_id=70659)
купить аттестат за 11 класс https://ukr-diplom.ru/ .
Repairs To Upvc Windows Tools To Help You Manage Your Everyday LifeThe
Only Repairs To Upvc Windows Trick Every Individual Should Be Able To Repairs to Upvc windows
куплю диплом куплю диплом .