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
Salutations, colleague reader. I must commend the author for their discerning and artfully-composed blog post.
The material was both illuminating and thought-provoking,
leaving me with a deeper understanding of the subject at hand.
I would aim to extend an invitation to engage with
the distinguished PUSSY888 network. This domain offers a
sphere of fun and intrigue, attracting those who esteem the more elegant things in life.
I exhort you to immerse yourself in the extensive array and engross yourself in the
electrifying adventures that summon you. Your participation would be extremely appreciated, and I await with
impatience the prospect to interact with you more intimately within this prestigious online
realm
My page: gambling trends
1с предприятие купить программу [url=www.svarog.forum24.ru/?1-0-0-00000330-000-0-0]www.svarog.forum24.ru/?1-0-0-00000330-000-0-0[/url] .
my canadian pharmacy review https://indiancertpharm.com/# Online pharmacy
india pharmacy
https://filedn.eu/lXvDNJGJo3S0aUrNKUTnNkb/marketing-257/research/je-tall-sf-marketing-(469).html
You should go for some bright colours, or if your daughter has a color picked out for you then go together with that.
Hi I am so grateful I found your webpage, I really found you by mistake, while I was looking on Digg for something else, Anyhow I
am here now and would just like to say thanks for a incredible post and a
all round entertaining blog (I also love the theme/design), I don’t have time to look over it all at the moment
but I have saved it and also included your RSS feeds, so when I have time I will
be back to read a lot more, Please do keep up the fantastic job.
Hi this is somewhat of off topic but I was wondering if blogs use WYSIWYG
editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience.
Any help would be enormously appreciated! http://Classicsmoothjazz.com/__media__/js/netsoltrademark.php?d=www.pnptech.co.kr%2Fbbs%2Fboard.php%3Fbo_table%3Dfree%26wr_id%3D531191
pharmacy canadian https://indiancertpharm.shop/# Indian Cert Pharm
п»їlegitimate online pharmacies india
This web site really has all the info I needed concerning this subject and didn’t know who to ask.
программа 1с купить с установкой программа 1с купить с установкой .
Thanks for the practical tips. More at بلفاروپلاستی چیست
I got this web site from my buddy who informed me about this site and at the moment this time I am
browsing this website and reading very informative content here.
купить программу 1с бухгалтерия купить программу 1с бухгалтерия .
Website https://toruswallet.org/
canada ed drugs: Canadian Md Pharm – best canadian pharmacy to order from
canadian drugstore online: Canadian Md Pharm – reliable canadian pharmacy reviews
online canadian pharmacy review: CanadianMdPharm – best canadian pharmacy online
Whiskey Bar 하이오피
“Ask Me Anything”: Ten Answers To Your Questions About Pragmatic Korea 프라그마틱
슬롯 환수율 (atomcraft.ru)
https://mexicaneasypharm.shop/# Mexican Easy Pharm
legal to buy prescription drugs from canada
программы фирмы 1с svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513 .
1с предприятие купить программу https://svarog.forum24.ru/?1-0-0-00000330-000-0-0 .
вывод из запоя стационар вывод из запоя стационар .
I pay a visit everyday a few blogs and sites
to read posts, except this blog provides feature based content.
Hello There. I found your blog using msn. This is a really well written article.
I will make sure to bookmark it and return to read more of your useful info.
Thanks for the post. I will certainly comeback.
настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .
настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .
Vi rekommenderar en strategi som riktar in sig på högsta möjliga volatilitet.
penis enlargement
here https://toruswallet.org
программы фирмы 1с программы фирмы 1с .