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
scripts rx pharmacy: Detrol – xenical dubai pharmacy
Guide To 3 Wheeler Pushchairs: The Intermediate
Guide On 3 Wheeler Pushchairs 3 wheeler Pushchair
We invite you to explore and contribute to the growth of this ecosystem.
cialis singapore pharmacy: mexican online pharmacy reviews – hy-vee pharmacy
The 9 Things Your Parents Taught You About Pvc Door Hinges pvc Door hinges
A Step-By Step Guide For Choosing Your Treehouse Canopy Treehouse Bunkbed
Are You Getting The Most From Your Mini Chest Freezer Uk?
chest freezer suitable for garage (Blake)
I was curious if you ever considered changing the layout of your blog?
Its very well written; I love what youve got to say. But maybe you could a little more in the way of
content so people could connect with it better. Youve got an awful lot of text for
only having one or 2 images. Maybe you could space it
out better?
good online pharmacy: australian pharmacy online – do pharmacy sell viagra
14 Misconceptions Common To Best Truck Accident Lawyer truck accident lawyer near me
konteyner | Bu konuyu bu kadar iyi açıklayan bir yazı bulmak zor. Tebrikler!
Thanks for the good writeup. It in truth was once a enjoyment account it.
Glance advanced to far brought agreeable from you!
By the way, how can we keep in touch?
buy amoxicillin no prescription fda checked pharmacy: express pharmacy – family rx pharmacy
Hi there! Someone in my Facebook group shared this website with us so
I came to give it a look. I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to
my followers! Superb blog and brilliant design.
5 Locked Keys In Car Service Lessons From The Pros
vehicle unlocking
First off I would like to say awesome blog! I had a quick question that I’d like to ask if you do not
mind. I was interested to find out how you center yourself and clear
your head prior to writing. I’ve had a hard time clearing my thoughts in getting my ideas out there.
I truly do take pleasure in writing however it just
seems like the first 10 to 15 minutes are wasted simply
just trying to figure out how to begin. Any recommendations or hints?
Many thanks!
clindamycin target pharmacy: tylenol pharmacy scholarship – pharmacy store locator
Hiya very cool blog!! Guy .. Excellent .. Superb .. I’ll
bookmark your blog and take the feeds also? I’m
glad to search out so many helpful information right here within the put up, we want develop extra techniques in this regard, thanks for sharing.
. . . . .
We are a group of volunteers and opening a new scheme in our community.
Your web site provided us with valuable information to work on. You’ve done an impressive job and our whole community
will be grateful to you.
Ledger Live
Активные игроки в покер получают шансы кликнуть на главное событие 2021 года
– https://kzpokerdomplus.com/
МЕ spf autumn!
You Are Responsible For An Adhd Private Assessment Budget?
12 Tips On How To Spend Your Money how do i Get an adhd assessment
konteyner fiyatları | Bu makale, konuya dair her türlü soruma yanıt verdi. Özellikle teknik detaylara verdiğiniz önem beni etkiledi.
When someone writes an piece of writing he/she keeps the idea of a user in his/her brain that how a user can be aware of it.
So that’s why this post is outstdanding. Thanks!
Hyzaar: ambien india pharmacy – mail order pharmacy no prescription
express rx pharmacy services: meds online – buy nolvadex online pharmacy
asacol online pharmacy: mexican pharmacy weight loss – giant food store pharmacy hours
Awesome! Its really amazing paragraph, I have got much clear idea about from this paragraph.
The 10 Scariest Things About Composite Door Repair Near
Me composite door Repair near me
Starlix: target store pharmacy hours – heb pharmacy online