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
продвижение сайтов в москве цена http://prodvizhenie-sajtov-v-moskve115.ru/ .
продвижение сайтов в москве в яндекс и гугл https://prodvizhenie-sajtov-v-moskve115.ru/ .
7 Tips About Misted Double Glazing Repairs Near Me That Nobody Can Tell You
handle for double glazed window (Janet)
where can i get generic duphalac without dr prescription
Good write-up, I am normal visitor of one?s website, maintain up the excellent operate, and It’s going to be a regular visitor for a lengthy time.
5 Laws That Can Benefit The Motor Vehicle Lawyer Industry Motor Vehicle accident lawyers
10 Wrong Answers To Common Vegas Slots Questions Do You Know The Right
Answers? entertaining Slots (frli.dk)
An asbestos lawsuit can be a way for the victim or their family members to receive compensation from
the companies responsible for their exposure. Compensation can take the form of a jury verdict or settlement.
My website – mesothelioma case
where can i buy cheap imdur without dr prescription
Drug information sheet. Short-Term Effects.
can you get cheap digoxin pill
All what you want to know about medicines. Read here.
I every time used to study paragraph in news papers but now as I
am a user of net therefore from now I am using net for content, thanks to
web.
Evryone loves what yoou guys are ᥙр toο. Tһis type of clever ѡork and coverage!
Қeep ᥙp thee superb wօrks guys Ι’ѵe incorporated yߋu guys to mу own blogroll.
Αlso visit my web site :: Arena333 Slot Login Link Alternatif
You’ll Never Guess This Window Replacement
Near Me’s Tricks Window replacement near me
Attorneys often prefer settlements because they are less risky, and clients can get compensation much faster.
They also take into consideration the cost of treatment and wrongful death awards during settlement negotiations.
Here is my web page – Asbestos Claim
Medicines information for patients. Short-Term Effects.
can i purchase generic sumatriptan online
Everything trends of medicine. Get now.
The 10 Most Terrifying Things About Online Grocery Stores That Ship online grocery stores that ship
Compelling and thought-provoking investigation of the
topic. Your examination was exhaustive and well-substantiated,
furnishing participants with a exhaustive comprehension of
the essential topics at grasp.
I may be overjoyed to communicate complementary on this subject.
If you agree, I would cordially request you to engage with us on the
SBOBET community, where we can sustain our dialogue in a elevated dynamic domain.
my website – online casino banking solutions (intensedebate.com)
Hi there just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Firefox.
I’m not sure if this is a format issue or something to do with internet
browser compatibility but I thought I’d post to let you know.
The design look great though! Hope you get the issue fixed
soon. Cheers
10 Things We All Do Not Like About Semi Truck Litigation semi truck Accident Lawyers
Window Repair Near Me Tips To Relax Your Everyday Lifethe Only Window Repair Near Me Technique Every
Person Needs To Learn Window Repair Near Me
Where had been you?” he asked. Before she could answer, she said, he made a
comment in regards to the very building she’d simply visited.
My web blog Custom jewellery travel pouches (Lakesha)
Lorem Ipsum has been the industry’s standard dummy text https://innocap-goettingen.de/kzqrgo of type and scrambled it to make a type specimen book.
11 Ways To Completely Sabotage Your Player Favorite Slots slot features
Ну, правда надо знать как и что делать) неумехам в этом плане вряд-ли что-то светит.
lisinopril 20 mg tablets
The Reason You Shouldn’t Think About How To Improve Your Leather
Couch real leather couch
See What Glazing Repairs London Tricks The Celebs Are Making Use Of glazing repairs london (tujuan.Grogol.us)
9 . What Your Parents Taught You About Upvc Windows Near Me
upvc windows near me
Pills prescribing information. Effects of Drug Abuse.
buying cheap reglan prices
Everything news about pills. Get now.