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://dickfix.ru/
Howdy! I understand this is somewhat off-topic but I needed to ask.
Does running a well-established website such as yours take a large amount of work?
I am brand new to writing a blog but I do write in my journal
daily. I’d like to start a blog so I can share my personal experience and views online.
Please let me know if you have any kind of suggestions or
tips for brand new aspiring bloggers. Appreciate it!
The Secret Secrets Of Best CSGO Case To Open danger zone case
What Is Best Linkbuilding Software And How To Utilize It
link builder Software
tamoxifen dose: does tamoxifen cause weight loss – should i take tamoxifen
Guide To Locked Myself Out Of My Car: The Intermediate Guide In Locked Myself Out Of My Car locked myself out of my car (wayranks.com)
buy cytotec online fast delivery: Misoprostol 200 mg buy online – buy cytotec pills online cheap
See What Best Rated Robot Vacuum Tricks The Celebs Are Utilizing Best Rated Robot Vacuum
What Semi Truck Lawyer Experts Want You To Be Able To semi truck accident attorneys (http://daywell.kr/)
http://ciprofloxacin.tech/# ciprofloxacin
lisinopril 1.25 lisinopril generic 20 mg lisinopril 10mg tablet
What Is The Reason Adding A Key Word To Your Life Will Make All The An Impact auto key replacement locksmith
Adhd Symptoms In Adult Women Explained In Less Than 140 Characters Symptoms For Adhd In Females
Great post. I was checking continuously this blog and I am impressed!
Extremely helpful information specially the last part :
) I care for such info much. I was looking for this certain info for a long time.
Thank you and best of luck.
Воєнторг
6. Тактические рюкзаки и сумки для военных
штани тактичні літні штани тактичні літні .
buy propecia without rx buy propecia without a prescription buying cheap propecia without prescription
Five Killer Quora Answers On Double Glazing Repairs Leeds
double glazing repair
https://nolvadex.life/# tamoxifen chemo
A Peek At Why Are CSGO Skins Going Up In Price’s Secrets Of Why Are CSGO Skins Going Up In Price cs2 cases – Margart –
There’s A Good And Bad About Fridge Freezer American american fridge freezer sale (Micah)
https://nolvadex.life/# aromatase inhibitors tamoxifen
10 Bag Prada Techniques All Experts Recommend cheaper
http://cytotec.club/# buy cytotec in usa
Hey There. I found your blog using msn. This is an extremely well
written article. I’ll make sure to bookmark
it and come back to read more of your useful information.
Thanks for the post. I’ll certainly return.
cost for 2 mg lisinopril prinivil 5 mg tablets buy lisinopril mexico
An Adventure Back In Time: What People Discussed
About Malpractice Compensation 20 Years Ago malpractice Lawyers
The History Of Locksmith For Auto In 10 Milestones auto keys locksmith
Верификация документов необходима
для вывода со счета сумм более долларов.
50 cg synthroid
14 Cartoons About Machine Espresso To Brighten Your
Day espresso machines