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
Some individuals is vape juice vegan
flavored pure nicotine
USP human development hormonal agent (somatropin).
Look into my web-site … Bookmarks
Your style is so unique compared to other folks I’ve read stuff from.
I appreciate you for posting when you have the opportunity,
Guess I will just bookmark this site.
NewULife Ranks. Invite to my NewUlife Evaluation.
Here is my web blog Bookmarks
Unquestionable Evidence That You Need Private ADHD Test private adhd Assessment wirral
In this case, the life insurance policy company’s cash.
My homepage; Bookmarks
Authentic HGH Perks (NewULife Hgh Gel Not Checked Yet).
Feel free to surf to my web-site … bookmarks
What is New U Life SOMADERM Transdermal Gel.
Also visit my blog post … ai headshot generator free review
What is New U Life SOMADERM Transdermal Gel.
Also visit my web-site; how to report content analysis results
Hi! I’ve been following your site for a long time now and finally got the
courage to go ahead and give you a shout out from Atascocita Texas!
Just wanted to tell you keep up the fantastic job!
The vapor and taste high quality is additionally terrific.
My homepage – Bookmarks
I’m the owner driver of GOPARTYBUS party bus perth hire.
Allow me reveal you in my testimonial of NewULife.
my site; Bookmarks
Drugs information sheet. Cautions.
order estradiol pill
Actual news about pills. Read here.
A synthetic USP human growth hormone (somatropin).
Look at my web page: Bookmarks
Hi, I read your blogs daily. Your humoristic style is witty, keep up the good work!
Some individuals vape flavorful pure nicotine
my blog post – what beers are vegan uk
Paragraph writing is also a fun, if you be acquainted with after that you can write otherwise it is difficult to
write.
Batik Slot
Are Repair Misted Double Glazing Near Me As Important As Everyone Says?
Fitters
How Cerebral Palsy Lawsuit Propelled To The Top Trend On Social Media Cerebral Palsy Lawyers
Wow, that’s what I was searching for, what a stuff!
present here at this weblog, thanks admin of this site.
A synthetic USP human growth hormone (somatropin).
Here is my page … types of disability benefits in canada
I am curious to find out what blog platform you have
been utilizing? I’m experiencing some small security problems with
my latest website and I’d like to find something more risk-free.
Do you have any solutions?
10 Reasons You’ll Need To Be Educated About Online Shopping Sites online Shopping sites for Dress
They have products to market or services to provide.
Also visit my webpage – content decay google
https://image.google.dz/url?q=https://krolik78.ru
Discovering a spanish language Learning apps Free does not
need to be costly.
Drugs prescribing information. What side effects can this medication cause?
get generic phenergan pills
Actual information about drugs. Read now.
Well I sincerely enjoyed studying it. This post offered by you is very
constructive for proper planning.
Feel free to visit my site :: best seo services in ahmedabad
I know this website provides quality based articles and other stuff, is there
any other web site which offers these kinds of information in quality?