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
What’s up everyone, it’s my first pay a quick visit at this site, and piece of writing is really
fruitful in support of me, keep up posting these types of
content.
12 Stats About Slot Games To Make You Take A Look At Other People
Slot machines (cse.google.co.za)
5 Private Psychiatrist Online Lessons From Professionals
Private Psychiatrist uk cost
Pills prescribing information. Long-Term Effects.
lyrica cost cvs
All about pills. Read here.
can i get lexapro no prescription
Great delivery. Great arguments. Keep up the amazing work.
продвижение сайтов в москве с оплатой за результат prodvizhenie-sajtov-v-moskve115.ru .
It’s very trouble-free to find out any topic on net as compared to books, as I found this piece
of writing at this web site.
продвижение сайтов в москве и московской области http://prodvizhenie-sajtov-v-moskve115.ru/ .
I am really loving the theme/design of your site. Do you ever
run into any browser compatibility issues? A
few of my blog readers have complained about my website not operating correctly in Explorer but looks great
in Firefox. Do you have any suggestions to help fix this problem?
15 Of The Best Pinterest Boards Of All Time About Auto Accident Attorneys auto accident lawsuit (Lucio)
поисковое сео продвижение сайтов в москве https://prodvizhenie-sajtov-v-moskve115.ru .
Pills prescribing information. Brand names.
can you get plan b online
Actual trends of meds. Get information now.
I have been surfing online more than 3 hours today, yet I never found any interesting article like yours.
It’s pretty worth enough for me. Personally, if all website owners
and bloggers made good content as you did, the web will be a lot more useful than ever before.
My web site: 토토친구
What’s The Job Market For Coffee Machines Beans Professionals?
coffee machines beans (Richard)
Medicines information leaflet. Short-Term Effects.
can i buy generic neurontin online
Everything news about medicament. Get information here.
cleocin davis pdf
You’ll Be Unable To Guess Combi Single Stroller’s Tricks single Stroller
The 10 Most Terrifying Things About Patio Doors Repairs Near Me Patio doors repairs
It’s awesome to visit this web page and reading the views of all colleagues regarding this article, while I
am also eager of getting familiarity.
Medicine information sheet. What side effects?
where can i get generic effexor without insurance
Best news about medicines. Get here.
Coffee Machine Nespresso Tools To Help You Manage Your Daily Life Coffee Machine Nespresso Technique Every
Person Needs To Know Machine Nespresso
If you’re prepared to get prequalified for a mortgage auto loan Calculator, we recommend speaking with Churchill Mortgage.
Drugs information. Drug Class.
where buy fluvoxamine
Everything trends of pills. Get information now.
There are few factors aas exciting as playing with the house’s oown cash.
Also visit my weeb site: 유동성
15 Motorcycle Accident Lawyers Benefits You Should All Be Able To Motorcycle accident lawsuits (jejucordelia.com)
Medication information leaflet. Short-Term Effects.
can you get colchicine prices
All what you want to know about medicament. Get information here.
5 Motives Slot Tournaments Can Be A Beneficial Thing
Top Developer Slots
Pills information leaflet. Cautions.
order cheap trileptal pills
Best what you want to know about drug. Read here.
Why American Fridge Freezer Uk Is Relevant 2023 http://www.zackfoxworth.top