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
купить диплом в пушкино diplomdarom.ru .
купить диплом о высшем образовании в самаре [url=https://man-diploms.ru/]man-diploms.ru[/url] .
7 Tips To Make The Most Out Of Your Citreon Key citroen relay Replacement key cost (https://mackay-goldstein-2.hubstack.net/what-you-can-do-to-get-more-out-of-your-citroen-c3-key-fob/)
generic ed pills low cost prescription ed remedies
Для тех, кто хочет грузоперевозки Новосибирск Томск, попутные маршруты обеспечат экономию.
buy prescription drugs from india: India pharmacy international – indian pharmacy online
ed pills that really work cheap pharmacy male erectile dysfunction
erectial disfunction https://indianpharm24.pro/# reputable indian pharmacies
купить диплом техникума в нижнем новгороде diplomdarom.ru .
Hey! I realize this is kind of off-topic however I had to ask.
Does running a well-established blog such as yours require a large amount of
work? I am completely new to writing a blog but I do
write in my journal everyday. I’d like to start a blog so I can easily share my own experience and views online.
Please let me know if you have any suggestions or tips for brand
new aspiring blog owners. Appreciate it!
диплом специалиста купить диплом специалиста купить .
можно ли поступить с купленным дипломом landik-diploms.ru .
The routes to trying to get a Canadian Gregory are
distinct for important people that applying. You’ll need to from the
form to see what merchandise is needed for you to fill the form.
Feel free to surf to my page – immigation; Gregory,
Keep on working, great job!
Hello my loved one! I want to say that this post is awesome, great written and include almost all vital infos.
I’d like to look more posts like this .
Treadmill Folding Incline Tips To Relax Your Everyday Lifethe Only Treadmill Folding Incline Trick That Everybody
Should Know treadmill Folding incline
See What Contemporary Pellet Stove Tricks The Celebs Are Utilizing contemporary pellet stove (Dixie)
Meds information for patients. Drug Class.
order generic bactrim prices
Some about pills. Read now.
купить аттестаты за 11 класс недорого купить аттестаты за 11 класс недорого .
Sports Toto Korea Tips From The Best In The Business 토지노 사이트, Google.Fm,
Fantastic items from you, man. I’ve consider your stuff
prior to and you are just too magnificent. I actually like what
you have got right here, certainly like what you’re saying and the best way by which you are saying it.
You’re making it enjoyable and you still take care of
to stay it wise. I can not wait to read much more from you.
This is actually a tremendous site.
15 Of The Top Sofas Sectionals Bloggers You Must
Follow apartment Size sectional sofa (voiceof.com)
Medicine information. Cautions.
where buy cheap risperdal pills
Everything information about medicines. Read now.
дипломная работа купить diplomdarom.ru .
10 L Shaped Chesterfield Sofa-Friendly Habits
To Be Healthy antique Chesterfield sofa
Kudos! I like it.
Hi there very nice blog!! Guy .. Beautiful ..
Wonderful .. I’ll bookmark your web site and take the feeds also?
I am glad to seek out so many helpful info right here in the post,
we need work out more strategies on this regard, thanks for sharing.
. . . . .
Wonderful beat ! I would like to apprentice while you amend your website, how could i subscribe
for a blog website? The account helped me a acceptable deal.
I had been tiny bit acquainted of this your broadcast offered
bright clear idea
I’m not that much of a internet reader to be honest
but your blogs really nice, keep it up! I’ll go ahead and bookmark your site to come back later on. Cheers
купить диплом о среднем образовании в чебоксарах diplomdarom.ru .