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
generic prednisone for sale
I’m not that much of a internet reader to be honest but your
blogs really nice, keep it up! I’ll go ahead and bookmark your site to come
back down the road. Cheers
casino
History Of Sectional Couches For Sale: The History Of Sectional Couches For Sale l Shaped leather Couch
(http://Www.google.Com)
buy cheap actos pills
надгробия гранитный уфалейские памятники
Three Reasons To Identify Why Your Best CSGO Opening Site
Isn’t Working (And How To Fix It) Counter-Strike Cases
This excellent website really has all the information and facts
I needed concerning this subject and didn’t know who to ask.
The Most Common Mistakes People Make With Which Is Best
For Online Grocery Shopping shopping online
buying generic tetracycline without insurance
Write more, thats all I have to say. Literally, it seems
as though you relied on the video to make your point.
You definitely know what youre talking about, why throw away your intelligence on just posting videos
to your weblog when you could be giving us something informative to read?
can i get generic lisinopril
Post writing is also a excitement, if you know after that you can write if not it is
difficult to write.
14 Savvy Ways To Spend On Leftover Online Shopping Sites Budget Online Shopping Sites For Dress – 49.Cholteth.Com,
excellent issues altogether, you simply received a new reader.
What would you suggest in regards to your post
that you made some days in the past? Any certain?
I am sure this article has touched all the internet viewers,
its really really pleasant piece of writing on building up new web site.
my homepage :: blackpass
Each gummy delivers 30 mg of broad-spectrum CBD.
My webpage; Bookmarks
Full-spectrum gummies contain trace amounts of THC.
Also visit my blog; Bookmarks
can i get generic super p-force prices
We’re a group of volunteers and starting a new
scheme in our community. Your site provided us
with valuable information to work on. You have done a formidable job and our entire community
will be thankful to you.
Today, we focus on the best buy full spectrum cbd gummies australia gummies
for sleep.
CBD may work better for anxiety than sleep.
Have a look at my blog post Bookmarks
The first is known as full-spectrum CBD oil.
Also visit my page: Bookmarks
The Ultimate Guide To Cerebral Palsy Claim Cerebral palsy lawyer
To connect with Joy Organics, log into Facebook.
Feel free to visit my page … berry CBD gummies for sleep issues
Allergic conjunctivitis clinical symptoms
First of all I would like to say great blog! I had a quick question which
I’d like to ask if you do not mind. I was interested to find out how you center yourself
and clear your head before writing. I have had trouble clearing my thoughts in getting
my ideas out. I truly do enjoy writing but it just seems like the
first 10 to 15 minutes are generally wasted simply
just trying to figure out how to begin. Any suggestions or tips?
Appreciate it!
современный конференц зал http://www.oborudovanie-konferenc-zalov11.ru .
Quality articles or reviews is the crucial to be a focus for the viewers to go
to see the website, that’s what this website is providing.
Joy Organics = 11.75 points. Joy Organics = 21 points.
Here is my homepage which cbd gummies help with pain