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
20 Things Only The Most Devoted Birth Injury Lawyers Fans
Know birth injury law firms; Polly,
Guide To Marc Jacobs Hand Bags: The Intermediate Guide To Marc Jacobs Hand Bags Marc jacobs hand bag
Howdy would you mind stating which blog platform you’re using?
I’m looking to start my own blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your layout seems different then most blogs and I’m looking for
something completely unique. P.S My apologies for
getting off-topic but I had to ask!
I love looking through an article that can make men and women think.
Also, thanks for permitting me to comment!
Ten Things You Learned In Kindergarden Which Will Aid You In Obtaining Mesothelioma Attorney Asbestos Lawyer
buy lisinopril without prescription: lisinopril 15 mg – cost of generic lisinopril
Hi, i think that i saw you visited my blog thus i came to “return the favor”.I’m trying to find things to enhance my web site!I suppose its ok
to use some of your ideas!!
nolvadex pills: lexapro and tamoxifen – tamoxifen rash
https://lisinopril.network/# cost of lisinopril 5 mg
buy cytotec online Cytotec 200mcg price Misoprostol 200 mg buy online
nolvadex steroids: dcis tamoxifen – tamoxifen citrate
What’s The Reason You’re Failing At Door Fitters Leeds glass replacement leeds
(Sylvia)
buy misoprostol over the counter buy cytotec over the counter Cytotec 200mcg price
http://finasteride.store/# order propecia without a prescription
https://ciprofloxacin.tech/# buy ciprofloxacin
https://finasteride.store/# cost generic propecia
9 Signs You’re An Expert Secondary Double Glazing Near Me Expert Windows
hysterectomy after breast cancer tamoxifen tamoxifen hip pain tamoxifen moa
It’s The Complete Cheat Sheet For Search Engine Optimisation Agency
search optimization
lisinopril tab 5 mg price: price of lisinopril 5mg – lisinopril pills for sale
http://cytotec.club/# purchase cytotec
lisinopril tablets india lisinopril brand name australia purchase lisinopril 40 mg
Five Killer Quora Answers On Tier 2 Seo Company Tier 2 seo
http://ciprofloxacin.tech/# cipro pharmacy
https://lisinopril.network/# lisinopril 7.5 mg
https://finasteride.store/# cost of generic propecia online
http://lisinopril.network/# zestril 20 mg tablet
https://lisinopril.network/# cost of lisinopril in canada
Hi! Do you use Twitter? I’d like to follow you if that would be okay.
I’m undoubtedly enjoying your blog and look forward to new posts.
Guide To Backlink Builder Software: The Intermediate Guide
In Backlink Builder Software backlink builder software (Nancy)