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
Pills information for patients. What side effects can this medication cause?
cheap ziprasidone without insurance
Actual about meds. Get information here.
Compelling and astute analysis of the subject.
Your examination was thorough and well-researched, furnishing readers with a complete appreciation of the main issues at access.
I would be delighted to engage extra on this content.
If you permit, I would willingly encourage you to sign up
for me on the SBOBET network, wherein we should continue our
debate in a elevated interactive arena.
My web blog; online casino ethical practices
Is Technology Making Slot Experiences Better Or Worse?
3d Slots (docs.inclick.net)
Greate article. Keep writing such kind of information on your page.
Im really impressed by your blog.
Hello there, You’ve performed an incredible job. I’ll certainly digg it and individually
recommend to my friends. I’m confident they will be benefited from this site.
Hi, after reading this awesome post i am also glad to share my knowledge here with
mates.
Forget Car Accident Litigation: 10 Reasons Why You Don’t Really Need It car Accident attorneys
10 Websites To Help You Learn To Be An Expert In Car Accident
Legal Car accident law Firms
This website was… how do you say it? Relevant!! Finally I’ve found something that helped
me. Many thanks!
Medicament information. Drug Class.
can you buy levaquin no prescription
Some information about drug. Get information here.
10 Inspiring Images About Dangerous Drugs Law Firm dangerous drugs law firms
” Nevertheless, results pertaining to the relationship between porn usage and sexuality are blended.”
Feel free to surf to my blog post … realtor fairfield bay
See What Window Replacement London Tricks The Celebs Are Using window replacement london
ADHD Traits In Women Tools To Improve Your Everyday Lifethe Only ADHD Traits In Women Trick That Every Person Must Learn adhd in men and women
Hi there to all, how is the whole thing, I think every one is getting more
from this website, and your views are nice in support of new visitors.
Medication information sheet. Brand names.
where can i buy generic cipro no prescription
Best trends of pills. Read here.
It’s The Complete List Of Upvc Repairs Near Me Dos And Don’ts Window Repair Near Me
I know this if off topic but I’m looking into starting my own weblog
and was curious what all is required to get setup?
I’m assuming having a blog like yours would cost a pretty
penny? I’m not very internet smart so I’m not 100% certain.
Any suggestions or advice would be greatly appreciated. Kudos
What’s The Reason? Classic Slots Is Everywhere This Year modern Slots
prednisone to buy online
where can i buy valtrex without rx
5 Killer Quora Answers To Shopping Online Sites List shopping online sites list – Jayson,
Excellent gοods from you, man. І һave takke intߋ accout your stuff prior to and
yоu aree just t᧐o fantastic. І atually ⅼike ᴡhat уօu һave acquired right here, гeally liike ѡһat уоu aare stating аnd thhe way
in ѡhich wheredin yߋu assert іt. Yoս are making iit enjoyable and
ʏߋu stiⅼl tаke care ߋf tо stay it smart. I can’twait too learn fɑr more
from үou. Tһіs iѕ aϲtually a tremendous site.
Feel free tߋ visit myy web-site :: Slot Gacor Hari ini
A Productive Rant About Free Slots best Payouts slots
buying cheap vasotec prices
cost of cheap doxycycline without rx
продвижение сайтов в москве яндекс оплата за результат https://prodvizhenie-sajtov-v-moskve115.ru/ .
продвижение и раскрутка сайтов в москве https://prodvizhenie-sajtov-v-moskve115.ru/ .
Post wгiting is also a fun, if you know after that you
can write otherwise іt is complicatеd to write.
Feel free to visit my website – Dedicatedproxies.Net
Drugs information sheet. Long-Term Effects.
where to buy generic zithromax no prescription
Best news about drugs. Read now.
Meds information leaflet. Short-Term Effects.
can i buy generic allegra without a prescription
All information about medicine. Read information now.