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
With thanks! Great stuff!
my web blog – http://donga-old.org/bbs/board.php?bo_table=free&wr_id=778574
Incredible points. Sound arguments. Keep up the good effort.
Medicines information. Cautions.
buy generic atarax pill
Some what you want to know about medicine. Get information here.
My family all the time say that I am killing my time
here at web, however I know I am getting know-how daily by reading such fastidious
posts.
5 Laws That Will Help With The Casino Slots Industry Perry
buy cheap cefixime without a prescription
buy generic singulair without prescription
The Steve Jobs Of Which Is The Best Online Supermarket Meet The Steve Jobs
Of The Which Is The Best Online Supermarket Industry best luxury online Shopping Sites uk
What’s The Job Market For Mesothelioma Law Firms Professionals?
mesothelioma Law firm
If you are going for most excellent contents like I do,
simply go to see this web page daily for the reason that it
offers quality contents, thanks
Its such as you read my mind! You seem to understand a
lot approximately this, such as you wrote the e-book in it or
something. I believe that you just could do with some p.c.
to force the message house a little bit, but instead of that,
that is magnificent blog. A great read. I’ll definitely be back.
What’s The Point Of Nobody Caring About Double Glazed Windows Repair misty
9 . What Your Parents Taught You About Double Glazed Windows Birmingham double glazed windows birmingham – Bertha –
An asbestos lawyer can help victims in filing a lawsuit against the
company which exposed them to asbestos.
The filing of a lawsuit may allow victims to recover compensation for medical
expenses, lost wages and other damages.
They have an all-time win percentage of .735, second highest in the NCAA.
my blog: http://sunshine2000.co.kr/bbs/board.php?bo_table=free&wr_id=30146
A Step-By-Step Guide To Slot Machines real Casino Slots (https://www.iztyz.com/)
Why Door Repairs Near Me Still Matters In 2023 upvc Door repairs near me (http://www.abc-iwaki.com)
Drug information leaflet. Long-Term Effects.
cost generic levonorgestrel prices
All about meds. Get here.
If some one needs expert view about blogging and site-building then i suggest him/her
to go to see this webpage, Keep up the nice work.
10 Things That Your Family Teach You About Shopping Online
Sites Shopping Online Sites (Vimeo.Com)
10 Life Lessons We Can Learn From Shopping Online Uk To Ireland cheap Online shopping uk clothes
The 10 Scariest Things About Which Is Best For Online Grocery Shopping which
is best for online grocery shopping (forum.igrarena.ru)
10 Things We All Are Hateful About Medication For ADHD And Anxiety Medication for adhd and depression
10 Things Everyone Hates About Accident Legal Accident law firms
15 Best Accident Compensation Bloggers You Must Follow
Accident lawyer
10 Reasons That People Are Hateful To Double Glazing Shops Near Me
Double Glazing Shops Near Me sash
The 3 Largest Disasters In Bed Couch Sectional History Sectional Sofas With Hide A Bed
This post is worth everyone’s attention. When can I find out more? https://Bookmarkinglog.com/story17369594/pied-confort
norpace uk – brand norpace buy chlorpromazine 50 mg sale
Zlatan Ibrahimovic https://zlatan-ibrahimovic.prostoprosport-br.com Bosnian pronunciation: ibraxi?mo?it?]; genus. 3 October 1981, Malmo, Sweden) is a Swedish footballer who played as a striker. Former captain of the Swedish national team.