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
Guide To Upvc Repairs Near Me: The Intermediate Guide Towards
Upvc Repairs Near Me upvc repairs near Me
https://ciprofloxacin.tech/# buy ciprofloxacin
The 10 Most Scariest Things About Electric Fireplace Wall Mounted electric fireplace Wall
7 Simple Secrets To Totally Intoxicating Your Private ADHD
Assessment Cost Uk Adhd Assessment Private
10 Factors To Know About Cerebral Palsy Litigation You Didn’t
Learn In The Classroom cerebral Palsy lawsuit
ciprofloxacin 500mg buy online: ciprofloxacin generic – buy cipro online canada
Are You In Search Of Inspiration? Check Out Online Shop Designer
Suits Oriental Furniture Fishbowl (Rosaura)
https://lisinopril.network/# lisinopril online without a prescription
buy lisinopril no prescription: lisinopril average cost – lisinopril 25
Greetings! I’ve been reading your blog for some time now and finally got the bravery to go ahead and give you a shout out from Porter Tx!
Just wanted to tell you keep up the good work!
10 Misconceptions That Your Boss May Have Concerning Online
Store Uk Cheapest 30G Desiccant Packs (https://vimeo.com)
The Reason Why Malpractice Claim Has Become Everyone’s Obsession In 2023
Malpractice Lawsuits
Five Killer Quora Answers To Window Repairman Window Repair
15 Fun And Wacky Hobbies That’ll Make You Better At Double Glazing Windows Repair Window Repair
http://lisinopril.network/# lisinopril 2.5
Why We Why We Best CS GO Case Opening Sites
(And You Should Too!) case glove (mensvault.men)
10 Things Everyone Hates About Autolocksmiths Nearby Auto Locksmith
Looking for a bitcoin casino wizard casino with plenty of games,
you should check out Casinoroyale.bet. The casino offers secure and fair gambling opportunities.
lisinopril metoprolol: lisinopril 40 mg without prescription – lisinopril online without prescription
cytotec abortion pill buy cytotec online buy cytotec over the counter
10 Misconceptions Your Boss Holds About Shopping Online Sites
List Luxury Bathroom Faucet – Dylan
–
lisinopril 2 how to buy lisinopril prinivil lisinopril
How To Choose The Right Defra Exempt Stove On The Internet Smoke control area stoves
I just like the helpful info you provide to your articles.
I’ll bookmark your blog and check again here regularly.
I’m relatively sure I will be told many new stuff proper here!
Good luck for the following!
my web blog – Aurum
подбор кода ТН ВЭД
lisinopril 20 mg price online: prinivil 10 mg – prinivil 5mg tablet
A Sage Piece Of Advice On Double Glazing In Leeds
From The Age Of Five Patio Door Repair Leeds
cytotec buy online usa: buy cytotec pills online cheap – buy cytotec pills
https://finasteride.store/# propecia brand name
cipro 500mg best prices: buy ciprofloxacin – cipro generic