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
NewULife Ranks. Invite to my NewUlife Review.
Here is my web site: Bookmarks
Hello! I just wanted to ask if you ever have any trouble with hackers?
My last blog (wordpress) was hacked and I ended up losing several weeks
of hard work due to no data backup. Do you have any
solutions to prevent hackers?
Mobile vaporizers use batteries as a source of power.
My homepage: plant based meat alternatives uk
Have you ever considered writing an ebook or guest authoring on other blogs?
I have a blog based upon on the same ideas you
discuss and would love to have you share some stories/information. I know my readers would
enjoy your work. If you’re even remotely interested, feel free to
shoot me an e-mail.
наркологическая помощь москва http://www.skoraya-narkologicheskaya-pomoshch11.ru .
you are in point of fact a good webmaster.
The site loading pace is amazing. It kind of feels that you’re doing any unique trick.
Furthermore, The contents are masterwork. you
have performed a excellent job on this topic!
mogadon online pharmacy: buy percocet online pharmacy – tesco pharmacy viagra 2012
bm pharmacy viagra: pharmacy store online – pharmacy magazine warfarin
What To Know About Crypto Trading
Официальная покупка школьного аттестата с упрощенным обучением в Москве
NewULife Ranks. Invite to my NewUlife Review.
Here is my site: small ceramic pottery wheel
wellbutrin indian pharmacy: methocarbamol online pharmacy – п»їlipitor copay card participating pharmacy
They have products to offer or services to supply.
Feel free to surf to my homepage … ranch wedding venues houston texas
снятие ломки клинике снятие ломки клинике .
Unlike various other professionals, we value your time.
Also visit my blog post: newulife shark tank
XYGENYX, a licensing firm for FDA-registered items.
My web page … Bookmarks
Hey I know this is off topic but I was wondering if you knew
of any widgets I could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time
and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading
your blog and I look forward to your new updates.
my blog: search engine optimization consultation
pharmacy one rx: online medicine tablets shopping – pharmacy online no prescription
USP human growth hormone (somatropin).
My blog post … bookmarks
Let me show you in my testimonial of NewULife.
Feel free to visit my page … orthopaedic surgeon specialist
The vapor and taste quality is additionally terrific.
Feel free to surf to my blog post; where can you buy vapes in australia
USP human development hormonal agent (somatropin).
Also visit my web-site – Foreign Auto Repair West Seattle
They have products to sell or services to offer.
Here is my website home theater system installation
NewULife Ranks. Welcome to my NewUlife Review.
Also visit my homepage :: Bookmarks
An artificial USP human growth hormone (somatropin).
My web blog Can i Order custom windows from home depot
XYGENYX, a licensing business for FDA-registered items.
Take a look at my blog; ceramic pottery mugs
XYGENYX, a licensing business for FDA-registered items.
Take a look at my web page; volkswagen service department hours
Genuine HGH Perks (NewULife Hgh Gel Not Checked Yet).
My webpage Bookmarks
XYGENYX, a licensing firm for FDA-registered items.
Have a look at my page: micro wedding venues in texas
Our competence reaches large business projects.
Feel free to surf to my web-site new u life sales