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
Misoprostol 200 mg buy online purchase cytotec cytotec online
nolvadex 10mg: tamoxifen joint pain – tamoxifen for breast cancer prevention
Hello There. I found your weblog the usage of msn. This is an extremely well written article.
I’ll be sure to bookmark it and return to
read more of your helpful info. Thank you for the
post. I’ll definitely comeback.
Why You Should Concentrate On Improving Mesothelioma Settlement asbestos Settlement
lisinopril 420 1g lisinopril medication otc price of lisinopril generic
Pretty part of content. I simply stumbled upon your blog
and in accession capital to assert that I acquire in fact loved account your weblog posts.
Any way I’ll be subscribing on your augment and even I success you get admission to
constantly fast.
An Adventure Back In Time What People Said About How To Get Cases In CS GO 20 Years Ago Case opening – maps.google.no,
Many Of The Common Errors People Make When Using Mesothelioma Navy
Compensation mesothelioma compensation claim (Oren)
The 10 Most Terrifying Things About Machine Espresso cafe espresso Machine
The Underrated Companies To In The Double Glazing Doctor Near Me
Industry double glazed window suppliers near me (Marsha)
https://nolvadex.life/# buy nolvadex online
matrixdestiny
tamoxifen moa tamoxifen pct nolvadex
buy cheap tadalafil online
cytotec online п»їcytotec pills online cytotec pills buy online
https://viagra-moscow.ru/
buy lisinopril 20 mg lisinopril 10 mg order online buy lisinopril 20 mg online usa
lisinopril tabs 10mg lisinopril 50 mg lisinopril pharmacy online
I constantly emailed this webpage post page to all my
friends, as if like to read it then my contacts will too.
The 9 Things Your Parents Taught You About Private Psychiatry Assessment Psychiatry Assessment
http://lisinopril.network/# buy 40 mg lisinopril
10 Things You Learned In Preschool, That’ll Aid You In Mesothelioma Case
Mesothelioma lawyer
My developeг is trying to persuade me tо move to .net from PHP.
I havе аlᴡays disliked the idea because of the costs.
But he’s tryiօng none the less. I’ve been using Movable-type on various weЬsіjtes for about
a yеar and am nervous about swіtching to another platform.
I have heard excellent things ɑbout blogengine.net. Is there a way I can transfer all
mу wordpress contеnt into it? Αny һelp would be greatly
apprecіated!
Feеel free tto surf to my page; kunjungi situs
5 Killer Quora Answers On Private Psychiatrist Assessment psychiatrist assessment (Freya)
20 Fun Informational Facts About Repairs To Double Glazed Windows Window repairs
The 10 Most Terrifying Things About Auto Accident
Law Firm Kansas City Auto Accident Lawyer
propecia buy cost cheap propecia without dr prescription order propecia without prescription
how to prevent hair loss while on tamoxifen: tamoxifen therapy – tamoxifen 20 mg tablet
The Best Tips You’ll Ever Receive On Which Beko Washing Machine
https://ciprofloxacin.tech/# ciprofloxacin order online