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
synthroid cheapest prices
10 Healthy ADHD Titration Waiting List Habits adhd titration private list (https://lovewiki.Faith)
I think what you said was very reasonable. However, think on this,
what if you wrote a catchier title? I mean,
I don’t wish to tell you how to run your website, however suppose you added a post title that makes
people desire more? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室
is a little boring. You could look at Yahoo’s
home page and see how they create news headlines
to get viewers interested. You might try adding a
video or a pic or two to get people interested about everything’ve written. In my
opinion, it might make your posts a little bit more interesting.
20 Fun Details About Mesothelioma Compensation Fund mesothelioma Lawyer compensation
Guide To Titration Meaning ADHD: The Intermediate Guide To Titration Meaning ADHD Titration Meaning Adhd (https://Historydb.Date)
This is a topic which is near to my heart…
Take care! Exactly where are your contact details though?
The Hidden Secrets Of London Online Mobile Shopping
Sites Flexzion Pond Filter System
cost generic propecia: get generic propecia price – generic propecia without dr prescription
http://nolvadex.life/# natural alternatives to tamoxifen
Lost My Car Keys No Spare Tools To Improve Your Everyday Lifethe Only
Lost My Car Keys No Spare Trick Every Person Should Be Able To lost My car keys no spare
10 Unexpected Online Jobs Work From Home Part Time Tips work From home job vacancy
7 Things About Leeds Door And Window You’ll Kick
Yourself For Not Knowing Leeds Double Glazing Repairs
10 Books To Read On Will CSGO Case Prices Go Down Clutch case [articlement.Com]
A Trip Back In Time What People Talked About Saab 93 Key Programming 20
Years Ago saab spare key
hey there and thank you for your information – I have certainly picked up something new from right here.
I did however expertise a few technical issues using this web site,
since I experienced to reload the website lots of times
previous to I could get it to load correctly. I had been wondering if
your web host is OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google
and could damage your high-quality score if advertising
and marketing with Adwords. Well I am adding this RSS to my e-mail and can look out for a lot more of your
respective exciting content. Ensure that you update this again soon.
https://natumbe.ru
Write more, thats all I have to say. Literally, it seems as though you relied on the video to
make your point. You clearly know what youre talking
about, why throw away your intelligence on just posting videos to
your blog when you could be giving us something enlightening to read?
Here’s A Little-Known Fact About Motor Vehicle Settlement.
Motor Vehicle Settlement motor Vehicle Accident attorney
https://ciprofloxacin.tech/# cipro online no prescription in the usa
Five Things You Didn’t Know About Best Online Shopping
Sites Clothes Bnus Color Mirror Sunglasses
Test: How Much Do You Know About Double Glazing Repair Leeds?
misted
Good day! I know this is kinda off topic nevertheless I’d figured I’d ask.
Would you be interested in trading links or maybe guest writing
a blog post or vice-versa? My website goes over a lot of the same subjects as yours and I feel we
could greatly benefit from each other. If you happen to be
interested feel free to send me an e-mail. I look forward to hearing from
you! Wonderful blog by the way!
A Step-By-Step Guide For Choosing Your Private ADHD Assessment Near Me Private adhd and Autism assessment near me
Remarkable! Its genuinely remarkable post, I have got much clear idea concerning from this post.
Tһis site really has all ⲟf the infoгmation I neeⅾed concerning this subject and didn’t knoѡ who to ask.
my weeb pаge jamuslot
What Is The Secret Life Of Cheap Cases CSGO case Esports
always i used to read smaller articles or reviews which also clear their motive,
and that is also happening with this article which I am reading now.
Guide To Replacement Key For Audi: The Intermediate Guide Towards Replacement Key For Audi key for audi
Railroad Injuries Lawyers Tools To Make Your Everyday Lifethe Only Railroad Injuries Lawyers Trick That Every Person Should Learn railroad Injuries lawyers
http://finasteride.store/# cost of generic propecia tablets
The 12 Types Of Twitter Window Repairs Leeds Tweets You
Follow repairs to Double glazed windows – http://www.ddhszz.com
–