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
https://indiaph24.store/# online shopping pharmacy india
I really like it when folks come together and share opinions.
Great blog, continue the good work!
This Is The History Of GSASER get gsa ser list
The Top 5 Reasons People Win Within The Medical
Malpractice Litigation Industry Medical malpractice lawsuit
5 Clarifications On Mesothelioma Legal Question asbestos claim
This post ѡill assist tһe internet ᥙsers for setting up new website οr evn a weblog ftom start too end.
Feel free tօ surf to mʏ web page: Slot Pulsa
10 Failing Answers To Common Small Espresso
Machine Questions Do You Know The Right Answers?
Espresso home machine
Ten Washing Dryer Combos That Really Make Your Life Better washer dryer Machine
There is certainly a great deal to know about
this issue. I really like all the points you’ve made.
https://indiaph24.store/# Online medicine order
Ten Upvc Window Repairs That Really Change Your Life Window repairs
It’ѕ ver effortless to find out any matter on net aas compared to books, as I
foսnd this post at this websitе.
The Evolution Of Bean Coffee Machine Coffee bean coffee machine
7 Secrets About Ford Key Repair That No One Will Tell You Ford Focus Key Replacement
Take a look at my web site facebook hookups
Hi Dear, are you genuinely visiting this site daily, if so afterward you will definitely take nice experience.
buy prescription drugs from india buy medicines from India Online medicine order
http://indiaph24.store/# indian pharmacy
Buzzwords De-Buzzed: 10 Other Ways To Say Washer & Dryer Combo washer & dryer combo
The 10 Scariest Things About Classic Casino Slots
Classic Casino slots
5 Laws That Will Help In The Repairs To Double Glazed Windows Industry
window Repairs
What To Focus On When Enhancing Mesothelioma Lawsuit asbestos lawsuit
Five Killer Quora Answers To Best Woodburners Best woodburners (http://www.mapleprimes.com)
How To Design And Create Successful Wall Mounted Fireplace Strategies From Home electric fireplaces on the
wall; http://isaevclub.ru/user/stephot3/,
http://canadaph24.pro/# canada drugs online review
https://mexicoph24.life/# medication from mexico pharmacy
Tһis design is steller! You obviously know how to keep a readеr amused.
Betrween your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!)
Great job. I really enjoyed what you had tto say, and more than that, how you presented іt.
Too cool! http://www.scullycap.com/comment/html/?246399.html
Is Top 10 Online Shopping Sites In Uk For Clothes As Important As Everyone Says?
Fast Roofing Coil Nailer
Key Reprogramming: 10 Things I’d Loved To Know
Earlier key programming (https://link-currie.federatedjournals.com/key-programmers-is-The-next-hot-thing-in-key-programmers/)
5 Killer Quora Answers To Walking Machine Under Desk Walking Machine Under Desk