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 records lesson blanketed with regards to natural carpets become attractive; thanks for enriching our mobile carpet cleaners
I couldn’t resist commenting. Perfectly written!
Как официально купить аттестат 11 класса с упрощенным обучением в Москве
prednisone 60 mg daily: Predni Best – prednisone price south africa
can i order cheap celebrex without insurance
I’m really enjoying the theme/design of your blog.
Do you ever run into any web browser compatibility issues?
A small number of my blog readers have complained about my site
not operating correctly in Explorer but looks great in Safari.
Do you have any recommendations to help fix this problem?
prednisone 20mg tablets where to buy prednisone buy cheap buy prednisone canadian pharmacy
J’ai récemment utilisé vos conseils avant d’appeler mon serrurier local ici à Bordeaux! pena serrurerie
Полезные советы по покупке диплома о высшем образовании без риска
free dark web darknet market dark market
Very soon this site will be famous amid all blogging and site-building people,
due to it’s pleasant posts
Как избежать рисков при покупке диплома колледжа или ПТУ в России
canadian online pharmacy prednisone Predni Best prednisone cost 10mg
I have been browsing online more than 3 hours
today, yet I never found any interesting article
like yours. It is pretty worth enough for me. In my opinion, if all webmasters and bloggers
made good content as you did, the internet will be much more useful than ever
before. https://365.Expresso.blog/question/la-clinique-sante-expert-a-st-louis-votre-partenaire-par-une-sante-optimale-18/
How You Can Use A Weekly ADHD Diagnosis Project Can Change Your Life adhd diagnosis statistics
my review here https://mangoandmoreshop.com/2024/06/13/strategi-menang-di-slot-jitu-rahasia-sukses-di-mesin-judi/
prednisone where can i buy: Predni Best – prednisone 30 mg daily
9 . What Your Parents Teach You About Male Mastubators male Mastubators
cipro ciprofloxacin ciprofloxacin 500mg buy online where can i buy cipro online
Case Battle The Process Isn’t As Hard As You Think Cs Case Battle
My experience with my car accident injury lawyer showed me just how important it is to have someone knowledgeable on your side during tough times
how to buy generic clomid: clomidonpharm – where to buy clomid tablets
How To Create Successful Train Accident Attorney Strategies From Home Trucking Accident Lawyers Near Me
where can i buy clomid without insurance: buying generic clomid without insurance – where can i get clomid pill
dark web market links darkmarket dark markets
Have you ever thought about adding a little bit more than just your articles?
I mean, what you say is fundamental and everything.
Nevertheless think about if you added some great visuals or video
clips to give your posts more, “pop”! Your content is excellent
but with pics and video clips, this blog could certainly be one of the very best in its niche.
Wonderful blog! https://Cl-system.jp/question/catalogue-fiche-produit-3/
I used to be recommended this blog via my cousin. I am now not positive whether this submit is
written by way of him as nobody else recognize such exact approximately my problem.
You’re incredible! Thanks! https://bdstarter.com/losteopathie-a-st-jean-quebec-une-approche-holistique-pour-votre-sante/
https://cipharmdelivery.com/# antibiotics cipro
I can’t stress enough how helpful an accident attorney was for my good friend after their accident. If you require legal aid, think about going to Giddens Law Firm
Полезные советы по покупке диплома о высшем образовании без риска