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
get cheap aldactone without rx
Wow that was strange. I just wrote an really long comment but after
I clicked submit my comment didn’t appear.
Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say excellent blog!
Three Of The Biggest Catastrophes In Workers Compensation Litigation History workers’ Compensation Attorneys
5 The 5 Reasons ADHD Adult Treatment Is Actually A Positive Thing adult Adhd Diagnosis and treatment
Lorem Ipsum has been the industry’s standard dummy text https://childrenfestivalatri.it/bdlbub of type and scrambled it to make a type specimen book.
doxycycline 50 mg side effects
Is Real Money Slots The Most Effective Thing That Ever Was?
Slot Sites
A Look At The Future What Is The Mesothelioma Compensation Industry Look
Like In 10 Years? Mesothelioma Lawyers
where can i get amaryl without prescription
Have you ever considered writing an e-book or guest authoring on other websites?
I have a blog based upon on the same information you discuss and would love to have you share some
stories/information. I know my readers would
value your work. If you are even remotely interested,
feel free to shoot me an email.
I was excited to uncover this website. I want to to thank
you for ones time due to this wonderful read!! I definitely loved every part of it
and I have you book-marked to check out new things in your site.
My page – lung nutra lung clear pro
can i purchase cheap feldene without rx
Howdy! Do you know if they make any plugins to safeguard against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?
My site – https://maryq906ojd8.westexwiki.com/user
Responsible For A Online Shopping Sites Uk Budget?
12 Best Ways To Spend Your Money 25 Ft Ethernet Cable
20 Fun Infographics About Window Repairs Near
Me Upvc window Repairs near me
Medication information leaflet. Generic Name.
increasing lisinopril 20 mg
Some what you want to know about meds. Read information now.
Awesome! Its in fact awesome post, I have got much clear
idea about from this post.
The 10 Most Scariest Things About 3 Seater Chaise 3 seater Chaise
My programmer is trying to persuade me to move to .net from PHP.
I have always disliked the idea because
of the costs. But he’s tryiong none the less. I’ve been using WordPress
on a number of websites for about a year and am worried about switching to another platform.
I have heard very good things about blogengine.net.
Is there a way I can transfer all my wordpress content into it?
Any help would be greatly appreciated!
Hurrah! In the end I got a webpage from where I
be able to truly take valuable facts regarding my study and knowledge.
Allergic conjunctivitis treatment tools
Missing, buckling, and curling shingles.If the roof is young, these damages may
be a sign that the roof is defective.
5 Killer Quora Answers To Charity Shop Online Clothes Uk charity shop Online clothes uk
7 Tips To Make The The Most Of Your Glass Repair Luton glass window repair
You’ll Be Unable To Guess Auto Accident Lawyers’s Secrets auto Accident
The 9 Things Your Parents Taught You About Slot Strategies progressive slots (Makayla)
Why No One Cares About Slot Games Online Slot Machines
What’s up, constantly i used to check website posts
here in the early hours in the daylight, because i enjoy to learn more and more.
Fastidious respond in return of this issue with solid arguments and telling the
whole thing concerning that.
can you buy generic feldene pills