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
buying generic clomid without a prescription: clomidonpharm – how can i get clomid price
https://prednibest.com/# buy prednisone no prescription
автоматические карнизы для штор автоматические карнизы для штор .
purchase amoxicillin online without prescription: Amox Star – amoxicillin order online
where can i buy amoxicillin over the counter uk: amoxicillin 30 capsules price – can you purchase amoxicillin online
Definitely believe that which you said. Your favorite reason appeared
to be on the internet the easiest thing to be aware of.
I say to you, I certainly get annoyed while people consider
worries that they plainly don’t know about.
You managed to hit the nail upon the top as well as defined out
the whole thing without having side-effects , people can take a signal.
Will likely be back to get more. Thanks
drug markets dark web darknet seiten darknet drugs
https://clomidonpharm.com/# can i order generic clomid tablets
where can i buy cipro online: CiPharmDelivery – where to buy cipro online
Hi mates, how is all, and what you desire to say
regarding this article, in my view its actually awesome designed
for me.
карниз с электроприводом для штор купить карниз с электроприводом для штор купить .
Asking questions are truly fastidious thing if
you are not understanding anything completely, but this post presents good understanding yet. http://h2Kelim.com/bbs/board.php?bo_table=free&wr_id=165630
On our website you will find unique collection rare coins from the whole planet. We offer both modern and historical specimens that will become real finds for collectors.
For more information, follow the link монета 10000 рублей 2024
Our team of experts carefully selects each coin, ensuring authenticity.
https://clomidonpharm.com/# where can i buy cheap clomid without prescription
https://prednibest.com/# prednisone 10 mg online
You’ll Never Guess This Tan Sectional Sofa’s Tricks Tan Sectional Sofa
The Reasons To Work With This Car Key Replacement Near Me Emergency Car Key Replacement Near Me
анонимный чат для вирта анонимный чат для вирта .
Nicely put. With thanks!
изготовление проекта перепланировки изготовление проекта перепланировки .
поролон стоимость поролон стоимость .
can i purchase generic clomid without prescription: can you get cheap clomid without insurance – cost generic clomid no prescription
I seriously love your site.. Very nice colors & theme. Did you create this amazing site yourself?
Please reply back as I’m hoping to create my own personal
blog and would like to find out where you got this from or
just what the theme is called. Appreciate it!
https://cipharmdelivery.com/# buy cipro without rx
Heya i’m for the first time here. I came across this board and I find It really
useful & it helped me out a lot. I hope to give something back and help others
like you aided me.
generic prednisone cost: Predni Best – can you buy prednisone over the counter
a While taking care oneself may seem indulgent remember prioritizing mental health requires proactive measures including engaging activities which replenish energy levels frequently lost during hectic schedules — find inspiration through best spas
Whɑt’s ᥙp, I logg onn tto you neew stuuff daily. Youur story-telling styke iss witty, eep itt up!
Loook into myy hompaɡe – پیشنهاد ویژه فقط امروز
Hi would you mind letting me know which web host you’re using?
I’ve loaded your blog in 3 different browsers and
I must say this blog loads a lot faster then most. Can you recommend
a good internet hosting provider at a fair price? Cheers, I appreciate it!