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
online pharmacy propecia prices: trusted online pharmacy – no rx pharmacy
tretinoin cream pharmacy: pharmacy2u propecia – mirtazapine online pharmacy
We’re a group of volunteers and starting a new scheme in our
community. Your site provided us with helpful info
to work on. You have done a formidable activity and
our entire neighborhood will be thankful to you.
An infant can have as many as 100 convulsions a
day.
Take a look at my homepage; infantile spasms with intractable epilepsy icd 10
Could Couch L Shaped Sofa Be The Key To Achieving 2023? Leather l shaped sectional
target pharmacy amoxicillin: ed pills online – med store pharmacy
The Reasons Buying A Mobility Scooter Is Everywhere This Year 6699101
Pretty part of content. I just stumbled upon your website
and in accession capital to say that I acquire in fact enjoyed account your weblog
posts. Anyway I will be subscribing on your augment or even I success
you get entry to persistently quickly.
It’s remarkable to pay a visit this web page and reading the views of all mates concerning
this piece of writing, while I am also zealous of getting
familiarity.
DHA Дизайн человека https://vkl-design.ru Дизайн человека. 6/3 Дизайн человека.
частная скорая наркологическая помощь частная скорая наркологическая помощь .
diplomat pharmacy lipitor: online medicine tablets shopping – publix pharmacy augmentin
OML Дизайн человека https://design-human.ru Дизайн человека. 3/5 Дизайн человека.
topiramate online pharmacy: provigil pharmacy online – bystolic pharmacy discount card
NCE Дизайн человека https://vkl-design.ru Дизайн человека. 5/1 Дизайн человека.
вывод из запоя цены на дому ростов вывод из запоя цены на дому ростов .
Why Nobody Cares About Robot Vacuum Best best Robot vacuum For the money
Our experience includes large-scale industrial projects.
My blog :: Bookmarks
doxycycline mexican pharmacy: percocet pharmacy – french pharmacy products online
связь с клиентами станет https://www.press-release.ru/branches/telecom/virtual-number-of-bahrain/ комфортнее, и бюджетнее.
loratadine pharmacy choice: kroger pharmacy store hours – online pharmacy furosemide
What’s The Good And Bad About Hinges For Upvc Doors how to replace window hinges
(Edmund)
Allow me show you in my evaluation of NewULife.
My web site; home theater seating
This article is truly a pleasant one it helps new net visitors, who are wishing for blogging.
I have read so many articles regarding the blogger lovers
but this piece of writing is in fact a nice article,
keep it up.
advair diskus online pharmacy: australia online pharmacy free shipping – all med pharmacy
The larger Slim battery, excellent for longer runtimes.
Also visit my website Bookmarks
скорая наркологическая помощь москва скорая наркологическая помощь москва .
They have items to sell or solutions to supply.
Stop by my web blog … home theater projector rca 1080p
USP human growth hormone (somatropin).
Feel free to visit my blog: window replacement near me car