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
It’s actually a nice and helpful piece of information. I’m glad that
you just shared this useful info with us. Please keep us informed like this.
Thanks for sharing.
USP human growth hormone (somatropin).
My page – Bookmarks
Wow, that’s what I was seeking for, what a data! present here at this web
site, thanks admin of this site.
Pretty! This was a really wonderful article. Thanks for supplying this information.
5 Killer Quora Answers To Replacement Double Glazing Units Near Me double glazing units near
me (images.google.com.sl)
What Experts From The Field Of Green Power Want You To Know electric wheel scooter
NewULife Ranks. Welcome to my NewUlife Evaluation.
Also visit my webpage what does enable need decay mean
Some individuals Vegetable Based Vape Juice flavorful nicotine
They have items to market or services to provide.
my blog Bookmarks
Mesothelioma Lawyer: The Good, The Bad, And The Ugly mesothelioma Lawsuit
You do NOT want to take in Lion’s Mane Mushrooms raw.
Stop by my blog post Bookmarks
I’m really enjoying the design and layout of your site.
It’s a very easy on the eyes which makes it much more pleasant for me to
come here and visit more often. Did you hire out a developer to create your theme?
Outstanding work!
Some people vape flavorful pure nicotine
Also visit my homepage – Bookmarks
Medicament prescribing information. Brand names.
buying generic zithromax without dr prescription
Best news about medicine. Read now.
What’s The Reason You’re Failing At Online Retailers Uk Stats
which supermarket is Cheapest for online shopping
USP human growth hormonal agent (somatropin).
My homepage stop motion animation online free
Pills prescribing information. Short-Term Effects.
can i get cheap atarax
Best trends of drug. Get now.
Online Shopping Websites Clothes Tools To Streamline
Your Daily Lifethe One Online Shopping Websites
Clothes Trick Every Person Should Learn Online Shopping Websites Clothes
Plant-based vapes have a host of possible advantages.
Also visit my web-site – Bookmarks
Some people vape flavorful nicotine
Here is my homepage; does plant based meat taste the same
I read this article fully regarding the resemblance of newest and earlier technologies, it’s remarkable article.
It’s an amazing piece of writing in favor of all the web
visitors; they will take advantage from it I am sure.
XYGENYX, a licensing business for FDA-registered products.
Here is my web site :: Bookmarks
Medicine information leaflet. What side effects can this medication cause?
cost of cheap reglan pills
Actual about medicament. Read information now.
You’ll Never Guess This Upvc Window Repairs’s Tricks upvc window repair
In this instance, the life insurance firm’s loan.
My web site – ai avatar generator free reddit
where to buy provera tablets
Drug prescribing information. Short-Term Effects.
cost levaquin no prescription
Some information about medicament. Get now.
Why 10 Kilo Washing Machine Is So Helpful During COVID-19 http://www.023456789.xyz
The vapor and flavor high quality is also terrific.
Take a look at my web page … Bookmarks