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
You’ll Never Guess This Adult Adhd Assessment’s Benefits Adhd Assessment
ролл апп https://www.rollap.ru .
https://finasteride.store/# cheap propecia tablets
Will Double Glazing Repair Near Me Ever Be The King Of
The World? wooden Windows double glazed
nolvadex 20mg tamoxifen hormone therapy tamoxifen hot flashes
I admire your piece of work, thankyou for all the informative content 서재응.
buy cytotec Abortion pills online buy cytotec
Seven Reasons To Explain Why Window Repair Near Is
Important fitters
Fantastic beat ! I wish to apprentice while you amend your site, how
could i subscribe for a blog site? The account helped me a acceptable deal.
I had been a little bit acquainted of this your broadcast offered bright clear idea
buy generic propecia without a prescription: cost cheap propecia price – cost of propecia
https://cytotec.club/# purchase cytotec
Guide To Upvc Repairs Near Me: The Intermediate Guide In Upvc Repairs Near Me
upvc repairs Near me
I’ve been exploring for a little for any
high-quality articles or blog posts on this kind of house .
Exploring in Yahoo I finally stumbled upon this
website. Reading this info So i’m satisfied to express that I’ve a very excellent uncanny feeling
I found out just what I needed. I such a lot unquestionably will make sure to do not forget this web site and provides it
a look on a relentless basis.
What’s The Job Market For Titration ADHD Adults Professionals?
Titration adhd
Who Is Responsible For An Mesothelioma Attorney Budget?
12 Tips On How To Spend Your Money asbestos compensation
buy cytotec online buy misoprostol over the counter order cytotec online
See What L30 Ultra Dreame Tricks The Celebs Are Using l30 Ultra dreame
can you buy prednisone without a prescription
http://cytotec.club/# order cytotec online
What’s The Current Job Market For Window Repairs Leeds Professionals Like?
window repairs leeds (Cheri)
Responsible For A Ghost Immobiliser Installation Budget?
10 Unfortunate Ways To Spend Your Money ghost Immobiliser installation
lisinopril 50 mg price: lisinopril price – zestoretic 20 25
11 Ways To Completely Redesign Your Car Keys Replacement
Near Me near By
The 3 Most Significant Disasters In Double Glazing Supplies Near Me The Double
Glazing Supplies Near Me’s 3 Biggest Disasters In History
pvc
http://cytotec.club/# cytotec buy online usa
Nine Things That Your Parent Taught You About
Glass Replacement Windows Glass Replacement Windows
Why No One Cares About Local SEO Experts affordable Local seo company
http://nolvadex.life/# where can i buy nolvadex
https://cytotec.club/# Abortion pills online
п»їbuy lisinopril 10 mg uk lisinopril 20 mg pill buy lisinopril without prescription