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
Medicine information leaflet. Brand names.
how to buy generic imodium without rx
Everything trends of medicament. Read information here.
The IQ is the most effective completely dry natural herb vape I
have actually attempted.
Also visit my website Bookmarks
5 Laws That’ll Help With The Online Shopping Uk Cheap Industry jolie papier Online shop uk amazon
An artificial USP human development hormonal agent (somatropin).
Check out my homepage – ai picture generator reddit free
I was recommended this website by way of my cousin. I am now not
sure whether this put up is written through him as no one else realize such detailed
about my difficulty. You’re wonderful! Thanks!
Experienced and specialist vehicle drivers.
Here is my site; Bookmarks
Asbestos may be exposed in many different ways.
However the majority of Asbestos case-related claims involve occupational exposure.
Yes, you can take in Lion’s Mane Mellow Mushroom branson reviews everyday.
Allow me reveal you in my evaluation of NewULife.
My blog post best ai headshot generator reddit
does plant based meat taste the same IQ is the very best dry
natural herb vape I have actually tried.
Meds information sheet. Cautions.
can i buy generic ramipril price
Best news about medication. Get information now.
10 Inspiring Images About Single Stroller Pushchair Single Pushchairs – http://www.samnetworkth.com –
15 Inspiring Facts About Examples Of Online Products That You Didn’t Know About online shop
I possibly edited the entire book editing software for windows 10 10 different times.
7 Tips About Asbestos Mesothelioma That Nobody Will Share
With You Cassy Lawn
Hi there everybody, here every one is sharing such experience, therefore it’s nice to read
this website, and I used to visit this website every day.
https://709h.com/space-uid-1042094.html
The Most Successful Double Glazing Windows High Wycombe Gurus Can Do 3 Things timber windows high wycombe
Lorem Ipsum has been the industry’s standard dummy text https://rampymobilne.pl/tuyclol of type and scrambled it to make a type specimen book.
Definitely imagine that which you stated. Your favourite reason seemed to be on the
web the easiest factor to understand of.
I say to you, I definitely get annoyed even as people consider concerns that
they plainly do not realize about. You managed to hit the nail upon the highest
as neatly as defined out the entire thing with no need side-effects , other
people could take a signal. Will likely be back to get more.
Thanks
Let me show you in my evaluation of NewULife.
Feel free to visit my web page Bookmarks
An artificial USP human growth hormonal agent (somatropin).
Feel free to surf to my blog; types of benefits disability
Finding out a language does not have to be costly.
my web page Bookmarks
The vapor and flavor quality is additionally great.
my page :: Bookmarks
Experienced and specialist motorists.
Feel free to surf to my site; party bus hire perth kids
The People Nearest To Window Repair Near Uncover Big Secrets window repair near me
(https://beta-doterra.myvoffice.com/Application/index.cfm?EnrollerID=458046&Theme=DefaultTheme&Returnurl=www.mazafakas.com/user/profile/4464374)
Portable vaporizers make use of batteries as a power source.
My page; Bookmarks
Five Killer Quora Answers On Shopping Online Uk Clothes
shopping online uk clothes [vimeo.com]
Allow me reveal you in my review of NewULife.
My web site: ai headshot generator cost
You’ve made some good points there. I looked on the web for additional information about the issue and found most people will
go along with your views on this site.