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
9 . What Your Parents Teach You About Repairing Bifold Doors repairing bifold
doors, imoodle.win,
Hey! Woul you mind if I share your blog with my myspace group?
There’s a lot of people that I think would really enjoy your content.
Please llet car driving service near me know.
Thank you
The 10 Scariest Things About Used 8mph Mobility Scooters Second Hand used 8mph mobility scooters Second hand
india pharmacy adipex: cyproheptadine online pharmacy – online pharmacy adipex
pop in this site and learn more http://tebelove.ru/ru/external-redirect?link=http://nspddfgurestv534.ru
How To Beat Your Boss Repair Double Glazing Jere Alas
store pharmacy: rx pharmacy generic viagra – viagra boots pharmacy
You said it perfectly.!
Hello, Neat post. There’s an issue with your website in web explorer, may check this? IE still is the marketplace chief and a large section of folks will pass over your fantastic writing due to this problem.
levitra pharmacy: online pharmacy cheap cialis – superdrug pharmacy viagra
ziprasidone online pharmacy: acyclovir online pharmacy – prescriptions online pharmacy
My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of
the expenses. But he’s tryiong none the less. I’ve been using Movable-type on a variety of websites for about a year and
am concerned about switching to another platform. I have heard
great things about blogengine.net. Is there a way I can import all my wordpress posts into
it? Any kind of help would be really appreciated!
See What Seat Leon Key Fob Tricks The Celebs Are Utilizing Seat Leon Key Fob
The History Of Getting An ADHD Diagnosis In 10 Milestones add diagnosis as an adult
What Is A Psychiatric Assessment Explained In Less Than 140
Characters getting a psychiatric assessment (cs-upgrade.top)
walgreen online pharmacy: ventolin mexican pharmacy – is rx pharmacy coupons legit
giant pharmacy store hours: avodart pharmacy – percocet in mexican pharmacy
What Is Mazda Key Replacement And How To Use It?
mazda keyless entry remote
A Good Rant About Adhd Assessment For Adults diva assessment for adhd
isotretinoin indian pharmacy: gen rx pharmacy – tesco pharmacy cialis
canadien pharmacies: harris teeter pharmacy – pharmacy online 365 legit
Howdy, i read your blog occasionally and i own a similar one and i
was just curious if you get a lot of spam remarks? If so how do
you protect against it, any plugin or anything you can suggest?
I get so much lately it’s driving me insane so any assistance is very much appreciated.
Et tenez-vous bien, 2024 risque bien d’être l’année
de Spice and Wolf ! Spice and Wolf suit l’histoire de Kraft Lawrence, un marchand
itinérant de 25 ans dans un monde fantastique
inspiré de l’Europe. C’est sûr, ça risque de faire du bruit et ça va remettre The Witch and the Beast
sous les projecteurs. 1. The Witch and The Beast :
Chasse à la sorcière ! On commence ce top manga anime 2024 avec The
Witch and the Beast. Dernière sortie de l’année dans cette catégorie, Babel, the
new Hakkenden, de Yûgo Ishikawa mêle l’ésotérisme et la
science-fiction à la perfection dans une des périodes historiques du
Japon : l’ère Eiroku, avec l’invasion de l’armée de Sadakane et la
famine qui règne de manière incessante. Classroom
Of The Elite, ça se passe dans un lycée super huppé, géré
par les big boss du gouvernement. Kazuma et sa bande, Aqua la déesse loufoque, Megumin l’explosive et Darkness la croisée maso, continuent leurs aventures dans ce monde parallèle.
Speak “Yes” To These 5 SEO Marketing London Tips plans
What Is Treatment For Adult ADHD And How To Use What Is Treatment For Adult
ADHD And How To Use adult Adhd diagnostic assessment and treatment
20 Fun Infographics About Small American Fridge Freezers american style fridge freezer cream (Marquita)
I was recommended this blog by my cousin. I am
not sure whether this post is written by him as nobody else know such detailed about my trouble.
You are incredible! Thanks!
o http://nl.jarfi.stephanegretry.net/blog/2024/08/05/como-encontrar-tempo-para-cassino-no-google-em-2021/ o betway online oferece mais, mais do que simplesmente online Slots.
all the time i used to read smaller articles or reviews
which as well clear their motive, and that is also happening
with this piece of writing which I am reading now.
Nine Things That Your Parent Teach You About Anxiety And Physical Symptoms 1738077