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
The 10 Most Scariest Things About Window Screen Replacement window screen Replacement
Where Will Prada Bag For Men Be 1 Year From Right Now?
Prada Bags gold
17 Reasons Why You Should Be Ignoring Search Engine Optimization search engine optimization website
(glk-egoza.ru)
http://lisinopril.network/# lisinopril medication
The 3 Greatest Moments In Mesothelioma History Mesothelioma Legal
The 10 Most Scariest Things About 18 Wheeler Wreck Lawyer 18 Wheeler Accident lawyer
17 Reasons You Shouldn’t Ignore Railroad Injuries Attorneys railroad injuries Lawsuits
You’ll Be Unable To Guess Automated Backlink Builder Software’s Tricks Backlink Builder Software,
Te.Legra.Ph,
10 Best Facebook Pages Of All Time About Motor Vehicle Attorneys auto
The People Who Are Closest To Combo Washing Dryer Share Some Big Secrets combo washing dryer
http://ciprofloxacin.tech/# ciprofloxacin generic price
I’m extremely impressed with your writing skills as neatly as with the
layout for your blog. Is that this a paid theme or did you customize it your self?
Either way stay up the excellent high quality writing, it’s uncommon to look a great
blog like this one today..
buy cytotec online fast delivery: Misoprostol 200 mg buy online – cytotec abortion pill
http://cytotec.club/# Abortion pills online
https://ciprofloxacin.tech/# buy cipro
Find Out What Folding Mobility Scooters The Celebs Are Using foldable mobility scooter lightweight
What’s The Job Market For Cerebral Palsy Attorney Professionals?
Cerebral Palsy Law Firms
15 Things You’re Not Sure Of About Avon Become A Rep Become An avon Rep online
http://cytotec.club/# buy cytotec online fast delivery
What You Need To Do On This Backlink Generator Software Ranker X
https://nolvadex.life/# effexor and tamoxifen
buy lisinopril online no prescription india how much is lisinopril zestril 30 mg
5 Common Myths About Truck Accident Attorney For Hire You Should Stay Clear Of Truck Accident Law Firms
Window Repairs Tips To Relax Your Daily Life Window Repairs Trick That Everybody Should Know Window repairs
buy cheap propecia online cost generic propecia no prescription buying cheap propecia
10 Tell-Tale Signals You Should Know To Know Before You Buy Double.Glazing Near Me double glazing windows
My spouse and I absolutely love your blog and find almost all of
your post’s to be exactly what I’m looking for.
Do you offer guest writers to write content for you personally?
I wouldn’t mind publishing a post or elaborating on some of the subjects
you write regarding here. Again, awesome weblog!
http://cytotec.club/# п»їcytotec pills online
lisinopril 5 mg india price: lisinopril coupon – zestril 10 mg price
9 Things Your Parents Teach You About Double Glazed Windows Near Me
double glazed windows Near me