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
http://indiamedfast.com/# IndiaMedFast
May I simply say what a comfort to discover someone that genuinely knows what they are discussing
on the net. You definitely know how to bring an issue to light and make it important.
More and more people need to look at this and understand this side of the story.
It’s surprising you aren’t more popular since you definitely have
the gift.
whoah this blog is excellent i like studying your articles.
Stay up the great work! You realize, many individuals are searching round for this information, you could help them greatly.
pdacenter.ru – сервис по ремонту бытовой техники
Ремонт электросамокатов в Иркутске в официальном сервисном центре PDACENTER.
Наши инженеры выполняют ремонт любой сложности по дотупным ценам!
The one difference between professional and common pills is that Viagra Professional comes within the type
of sublingual tablets. Is there a difference
between the chemical formula of Viagra for males and Viagra for girls?
There isn’t any such thing as homemade Viagra. The first thing to test if Viagra isn’t working is acceptable dosing.
Sildenafil was first introduced to the market by Pfizer in 1998,
as Viagra. 1993: Intel releases the primary Pentium processor.
This condition is characterised by problem in attaining or maintaining an erection suitable for sexual activity.
This could lead to blaming themselves and thinking
that they are the issue when in actuality it is simply a bodily condition generally known as erectile
dysfunction. You are not alone as round 1 million men in Australia
are affected by a rectal dysfunction. Viagra is a medicine designed that will help
you get and maintain an erection if you have erectile dysfunction (ED).
This will increase blood move to your penis to get or maintain an erection. They
are part of a category of medications called phosphodiesterase-5 (PDE5) inhibitors that
work by rising blood movement to the penis. Why doesn’t Viagra work for everyone?
If Viagra doesn’t work as it ought to or stops working, you’ll
be able to take some steps to give the drug its
finest probability to work.
Thanks for the informative post. More at obtenez plus d’info
ios iphone iphone cases
Starda Casino is a modern online gaming portal offering an exciting world of gambling entertainment. Anyone looking for quality slots, card games and sports betting will find everything they need for entertainment and, perhaps, winning Starda Casino
The realm of digital betting is continually advancing,
with innovative platforms, functions, and possibilities appearing each year.
cheapest online pharmacy india: India Med Fast – india pharmacy without prescription
dark websites https://github.com/aresmarketdarknetl9khn/aresmarketdarknet – dark market url
http://interpharmonline.com/# canada rx pharmacy world
https://indiamedfast.com/# India Med Fast
canadian pharmacy uk delivery
1вин кг 1вин кг .
Very nice post. I just stumbled upon your blog and wanted to
say that I’ve really enjoyed browsing your blog posts.
After all I will be subscribing to your rss feed and I hope you
write again soon!
cheapest online pharmacy india online medicine shopping in india online medicine shopping in india
https://interpharmonline.com/# drugs from canada
canada pharmacy online
Yes! Finally someone writes about Porn.
вход 1win вход 1win .
mexican pharmacy online: mexican pharmacy online – Mexican Pharm Inter
IndiaMedFast: india pharmacy without prescription – india pharmacy without prescription
https://mexicanpharminter.shop/# buying from online mexican pharmacy
I all the time used to read article in news papers but now as I am a user of net thus from now I am using net for articles,
thanks to web.
canadian 24 hour pharmacy: canada pharmacy no prescription – real canadian pharmacy
It’s enormous that you are getting ideas from this paragraph
as well as from our argument made here.
Awesome article.
https://mexicanpharminter.com/# mexican pharmacy online
canadian valley pharmacy: Online pharmacy USA – canadian pharmacy no scripts
reputable canadian online pharmacies highest rated canadian online pharmacy canadian drug prices
https://interpharmonline.shop/# vipps canadian pharmacy