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
Hi there, I desire to subscribe for this website to
obtain newest updates, therefore where can i do it please assist.
конференц залы оборудование конференц залы оборудование .
My spouse and I stumbled over here from a different web page
and thought I might check things out. I like what I see
so i am just following you. Look forward to looking at your web page for a second time.
Find Out What Demo Hades Tricks The Celebs Are Using oscarreys
It’s Enough! 15 Things About Double Glazing Repair Near Me We’re Sick
Of Hearing double glazing near me – vocerh.abril.com.br
–
Wow, amazing blog layout! How lengthy have you ever been blogging
for? you made running a blog look easy. The whole look of
your web site is great, let alone the content!
Genuine HGH Benefits (NewULife Hgh Gel Not Tested Yet).
My web-site; content analysis report example
It’s The Perfect Time To Broaden Your Upvc Repairs Near Me Options Window Repair Near Me
See What Double Glazing Crawley Tricks The Celebs Are Making Use Of double Glazing Crawley
Why You Must Experience Door Fitting Birmingham At
The Very Least Once In Your Lifetime birmingham specialist glass
Simply want to say your article is as astonishing.
The clarity in your submit is simply cool and i could suppose
you are an expert on this subject. Well with your permission let me to clutch your
RSS feed to keep updated with impending post.
Thank you 1,000,000 and please keep up the rewarding work.
can i purchase cabgolin without a prescription
You could certainly see your enthusiasm in the work you write.
The sector hopes for even more passionate writers such
as you who aren’t afraid to mention how they believe.
At all times follow your heart.
lisinopril dosage 2.5 mg
The vapor and taste top quality is additionally terrific.
My web site; plant based vape
Please let me know if you’re looking for a author for your weblog.
You have some really great articles and I think
I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some articles for your blog in exchange for a link back to mine.
Please send me an email if interested. Many thanks!
Thanks for finally talking about > JS 依照元素出現數量排序 –
馬老師 雲端研究室 < Loved it!
Authentic HGH Perks (NewULife Hgh Gel Not Examined Yet).
Also visit my web page: types of va disability benefits
Hello there! This is kind of off topic but I need some advice from an established blog.
Is it very hard to set up your own blog? I’m not
very techincal but I can figure things out pretty quick.
I’m thinking about setting up my own but I’m not sure where to start.
Do you have any ideas or suggestions? Cheers
The Reason Why Window Repair Near Is Everyone’s Obsession In 2023 Upvc Windows Repair Near Me
can i get generic tegretol no prescription
конференц залы оснащение конференц залы оснащение .
Great post! We are linking to this particularly great
article on our website. Keep up the great writing.
современный конференц зал https://www.oborudovanie-konferenc-zalov11.ru .
конференц-залы под ключ конференц-залы под ключ .
Discovering a language does not have to be expensive.
Also visit my blog :: Bookmarks
You do NOT want to consume Lion’s Hair Mushrooms raw.
my homepage :: Bookmarks
See What Glass Repair Leeds Tricks The Celebs Are Utilizing Glass Repair leeds (http://www.N49.com)
Lorem Ipsum has been the industry’s standard dummy text https://lacalabrianeifrancobolli.it/uvvqu of type and scrambled it to make a type specimen book.
Nine Things That Your Parent Teach You About Glass Replacement Windows Glass replacement windows