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
You’ll Never Be Able To Figure Out This Upvc Window Repairs
Near Me’s Benefits upvc window repairs near me (ww-w.loveclock.co.kr)
9 . What Your Parents Teach You About Auto Accident Lawsuit Auto Accident
Guide To Railroad Injuries Litigation: The Intermediate Guide In Railroad
Injuries Litigation Railroad Injuries
What’s The Job Market For Double Glazing Fitters Near Me
Professionals? double Glazing fitters
gay porn
What Is Double Glazing Glass Replacement Near Me And How To Utilize What Is Double Glazing Glass Replacement Near Me And How To Use replace Double glazing Glass
The Reasons Best American Fridge Freezers Could Be Your Next Big Obsession Zack Foxworth
can you buy accutane tablets
Quiz: How Much Do You Know About Auto Folding 4 Wheel Mobility
Scooter? Automatic Folding scooter
get cheap artane for sale
how to get cheap clomid pills
This Week’s Top Stories About Federal Employers Liability Federal Employers
Liability Fela Attorneys
Guide To ADHD Test Adults: The Intermediate Guide To ADHD
Test Adults Adhd Test Adults
Gareth Frank Bale https://garethbale.prostoprosport-br.com Jogador de futebol gales que atuou como ala. Ele jogou na selecao galesa. Ele se destacou pela alta velocidade e um golpe bem colocado. Artilheiro (41 gols) e recordista de partidas disputadas (111) na historia da selecao.
When I originally left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on each time a comment is added I
get 4 emails with the exact same comment. There has to be a means
you are able to remove me from that service? Thanks!
Robert Lewandowski https://robert-lewandowski.prostoprosport-br.com e um futebolista polones, atacante do clube espanhol Barcelona e capitao da selecao polonesa. Considerado um dos melhores atacantes do mundo. Cavaleiro da Cruz do Comandante da Ordem do Renascimento da Polonia.
What Do You Do To Know If You’re Prepared For Erb’s Palsy Claim erb’s palsy Lawsuit
I know this website presents quality dependent posts and
additional material, is there any other site which provides these kinds of data in quality?
Ten U Shaped Leather Couch That Will Actually Improve Your Life Small U Shaped Kitchen Ideas
The Best Place To Research Window Repair Luton Online Door Repair
buy prednisone 5mg online cheap
9 Lessons Your Parents Teach You About France Online Shopping
Sites Clothes France Online Shopping Sites Clothes
What The Heck What Exactly Is Best Slot Machines? Multi-Line Slots;
http://Www.Google.Com,
3 Ways In Which The Personal Injury Case Will Influence Your Life Personal Injury Lawyer
I’ve been exploring for a little bit for any high quality articles or blog posts
on this sort of space . Exploring in Yahoo I ultimately stumbled upon this
site. Reading this info So i’m happy to show that
I’ve a very excellent uncanny feeling I found out exactly what I needed.
I so much unquestionably will make sure to do not fail to
remember this website and provides it a look regularly.
11 Ways To Completely Revamp Your Car Accident Claim Car accident Law firms
get nemasole for sale
You can file a lawsuit against anyone who’s asbestos products caused harm.
After years of litigation, asbestos-related companies industry have declared
bankruptcy and created Asbestos Lawsuit bankruptcy trusts.
Good day! I know this is kinda off topic however I’d figured I’d ask.
Would you be interested in trading links or maybe guest authoring a
blog post or vice-versa? My blog covers a lot of the same subjects
as yours and I believe we could greatly benefit from each other.
If you’re interested feel free to send me an email.
I look forward to hearing from you! Wonderful blog by
the way!
You Are Responsible For The Cerebral Palsy Attorney Budget?
12 Ways To Spend Your Money Cerebral Palsy Law Firm