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
amoxicillin online purchase where can i buy amoxicillin over the counter amoxicillin 1000 mg capsule
Watch This: How Key Fob Repair Near Me Is Taking Over The World And What You Can Do About It car remote key repair shop near me – Eloy
–
gabapentin online prescription price for neurontin neurontin 300 600 mg
neurontin cap 300mg: canada neurontin 100mg lowest price – neurontin singapore
order prednisone 10 mg tablet: prednisone pills cost – buy prednisone canada
http://amoxila.pro/# amoxicillin 500mg no prescription
azithromycin cost india
prednisone 50 mg tablet canada: prednisone 5 mg brand name – prednisone 20mg buy online
buying amoxicillin in mexico amoxicillin 500 mg tablets amoxicillin 500mg capsule buy online
You really make it seem so easy with your presentation but I find this matter to be actually something that I think I
would never understand. It seems too complicated and very broad for me.
I am looking forward for your next post, I will try to get the hang of it!
Drugs information. Generic Name.
celebrex
Some news about medicine. Get here.
The Reasons To Focus On The Improvement Of CSGO Cases New kilowatt Case
10 Things That Your Family Teach You About Bentley Continental
Gt Key Fob bentley continental gt key fob
Genuine HGH Perks (NewULife Hgh Gel Not Evaluated Yet).
My blog post rhodium price chart kitco
10 Websites To Help You Learn To Be An Expert In Malpractice
Attorneys malpractice law Firm
5 Tools Everyone Is In The Slot Machines
Industry Should Be Utilizing legitimate Slots
buy amoxicillin 500mg online amoxicillin 875 125 mg tab buy amoxicillin 500mg capsules uk
Undeniably believe that which you stated. Your favorite reason seemed to be
on the web the easiest thing to be aware of. I say to you, I definitely get annoyed while
people consider worries that they plainly do
not know about. You managed to hit the nail upon the top
and also defined out the whole thing without having side-effects , people
can take a signal. Will probably be back to get more.
Thanks
Guide To Robot Vacuum For Pet Hair: The Intermediate Guide
For Robot Vacuum For Pet Hair robot vacuum for pet hair (https://qooh.me/crowdbrass85)
http://gabapentinneurontin.pro/# brand name neurontin price
amoxil generic: buy amoxil – over the counter amoxicillin
What Is Planet Spa Pillow Mist? What Are The Benefits And How To Utilize It avon planet spa radiant gold
neurontin 100 neurontin 2018 buy cheap neurontin online
10 Myths Your Boss Is Spreading Regarding Private ADHD Assessment Cost
how Much does a private adhd assessment cost
10 Facts About Locksmith Car That Insists On Putting You In The Best Mood Mobile car key locksmith near me (notabug.Org)
prednisone 1 tablet prednisone 50mg cost purchase prednisone
10 Things People Hate About Railroad Injuries Attorneys Thibodaux Railroad Injuries Attorney
Wow, amazing weblog structure! How lengthy have you
ever been running a blog for? you made blogging
look easy. The entire look of your website
is wonderful, as smartly as the content material!
See What Dreame Bot L30 Ultra Tricks The Celebs Are Using dreame bot l30 ultra
http://gabapentinneurontin.pro/# neurontin prices generic