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
The Advanced Guide To Medical Malpractice
Claim medical malpractice lawyers (Penelope)
15 Inspiring Facts About Zeus Slot Machines That You Never Knew zeus 138 Demo
20 Resources That Will Make You More Efficient At Dangerous Drugs Law Firm dangerous drugs attorney
Counter Strike It’s Not As Expensive As You Think counter strike Global Offensive
Making use of for this loan on-line is easy and quick.
15 Gifts For The Malpractice Attorneys Lover In Your Life malpractice law Firm
A Complete Guide To Online Shopping Uk Electronics Moen Rust-Resistant Shower Rod
Hi, i believe that i saw you visited my web site thus i got here to go back the choose?.I’m
attempting to to find issues to enhance my web site!I assume its good enough to use a few of
your concepts!!
I think the admin of this web site is actually working
hard in support of his web page, because here every information is quality
based material.
시알리스 구매
시알리스(Cialis)는 비아그라와 유사한 성기능개선제로, 남성의 발기부전 치료에 사용되는 약물입니다. 시알리스의 주요 활성 성분은 타닐라필(타닐라필로)이라는 화합물입니다.
The Most Effective Double Glazed Window Repair Tricks To Change Your Life window repairs near me (Franklin)
10 Things That Your Family Taught You About Online Famous Shopping
Sites online famous shopping sites
Meds information for patients. Effects of Drug Abuse.
cost generic coreg without a prescription
Some what you want to know about medication. Get information here.
купить диплом кандидата наук http://ukr-diplom.ru/ .
Какой ламинат чаще всего используют в косметическом ремонте квартиры https://instrument33.ru/vybor/kakoj-laminat-chashhe-vsego-ispolzuyut-v-kosmeticheskom-remonte-kvartiry/
I visited multiple web pages however the audio feature for audio songs existing at
this web site is genuinely wonderful.
10 Instagram Accounts On Pinterest To Follow About Boat Accident Compensation boat accident Attorneys
10 Wrong Answers To Common Window Screen Replacement Questions Do
You Know The Right Answers? Window Replacement Glass
How To Build A Successful New Slots Online When You’re Not Business-Savvy latest slots (telegra.ph)
3 Ways The Double Glazed Units Near Me Can Influence Your Life Replacement double Glazed Units near Me
Three Of The Biggest Catastrophes In Uk Online Phone Shopping Sites The Uk Online Phone Shopping Sites’s 3
Biggest Disasters In History Relief Kitchen Mat
[Angela]
Рейтинг ремонтных компаний https://interra-forum.ru/dizajn/vybor-kompanii-dlya-kompleksnogo-remonta-kvartiry-glavnye-faktory/
Какой ламинат чаще всего используют в косметическом ремонте квартиры https://instrument33.ru/vybor/kakoj-laminat-chashhe-vsego-ispolzuyut-v-kosmeticheskom-remonte-kvartiry/
pharmacy dispensing clozaril
где купить диплом http://ukr-diplom.ru/ .
What’s up to all, since I am in fact eager of reading this webpage’s post
to be updated regularly. It carries fastidious data.
Visit Tramadol Source – https://rx2go.com/buy/tramadol/
A Productive Rant Concerning Slot Demo Demo Slot x 500
купить диплом вуза https://ukr-diplom.ru .
There Are A Few Reasons That People Can Succeed At The High Variance Slots
Industry slots with bonuses
비아그라 약국 가격
비아그라 약국 가격: 비아그라 구매 비용과 가격 비교