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
Some people vape flavorful nicotine
Feel free to surf to my homepage – Bookmarks
You can take Hunter Emphasis with various other supplements.
my website :: review mushroom head air cushion cc cream
NewULife Ranks. Invite to my NewUlife Evaluation.
my web blog – Bookmarks
Medicine information for patients. Generic Name.
order rogaine pill
All news about drug. Read now.
http://googlepagerankgurus.com/__media__/js/netsoltrademark.php?d=https://generikonline.ru
Wonderful work! This is the type of info that should be shared around the web. Shame on Google for not positioning this post higher! Come on over and visit my site . Thanks =)
I am curious to find out what blog platform you happen to be working with?
I’m experiencing some small security problems with my latest website and I’d like to find something more risk-free.
Do you have any suggestions?
Hell᧐, I enjoy reading through ʏⲟur post.
I ⅼike to write a littⅼe commеnt to support y᧐u.
Visit mʏ blog – Slot Deposit 5k
You’ll Never Guess This Greenpower Mobility Scooters’s Tricks Greenpower Mobility
Naast valbeveiliging zijn err nog andere belangrijke maatregelen.
This cbd gummies for ed for sale near me brand also carries oils
and treats for pets.
Lawyers Near Me Tools To Ease Your Everyday Lifethe Only Truck Accident Lawyers Near
Me Trick Every Person Should Learn truck Accident
Drugs information leaflet. Effects of Drug Abuse.
cost of cheap neurontin without dr prescription
Actual news about pills. Get information now.
Thanks a lot! I like this.
Here is my website: https://variety.com/2024/biz/news/andrew-huberman-wme-1236008293/
Plant-based vapes have a host of potential advantages.
Look into my webpage what beers are vegan uk
How To Make A Profitable Cerebral Palsy Settlement
Entrepreneur Even If You’re Not Business-Savvy
Cerebral palsy attorney
Pills information sheet. Effects of Drug Abuse.
coumadin pills
Everything information about medicines. Read now.
Anxiety Disorders Types Tips To Relax Your Everyday Lifethe Only Anxiety
Disorders Types Trick Every Person Should Learn Anxiety disorders types
See What Where To Buy Electronics Online Tricks The Celebs Are Utilizing Where to buy electronics online
XYGENYX, a licensing business for FDA-registered
items.
Here is my webpage; a-i generator Reviews
Hiya νery cool web site!! Guy .. Excellent ..
Wonderful .. Ι will bookmark ʏour website ɑnd take tһe feeds
ɑlso? I’m glad too search out numerous helpful info here within tһe submit, ᴡe’d
like wоrk out moore techniques on this regard, thankѕ ffor sharing.
. . . . .
my web ρage – slot online gacor hari ini
Greetings! Very useful advice in this particular article!
It’s the little changes that produce the greatest changes.
Thanks a lot for sharing!
Have a loook at my page: Find more information
The 9 Things Your Parents Teach You About Double Glazed Windows Near Me double glazed windows near me
An artificial USP human growth hormone (somatropin).
Feel free to surf to my blog post: types of federal disability benefits
How To Resolve Issues With Double.Glazing Near Me replacing double Glazed units
(Tourmin.co.kr)
10 Facts About Mesothelioma Lawyer That Can Instantly Put You
In Good Mood mesothelioma Claim
Online Shopping Website In London Tools To Ease Your Daily Lifethe
One Online Shopping Website In London Trick That Every
Person Should Know online Shopping website in london
Drugs information leaflet. Generic Name.
where to buy tamoxifen without a prescription
Some about meds. Get information now.
Hi, i think that i saw you visited my blog so i came to “return the favor”.I am trying to find things to
enhance my site!I suppose its ok to use some of your ideas!!
Plant-based vapes have a host of possible advantages.
Also visit my site Bookmarks