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
10 Things That Your Family Taught You About Veterans Disability Lawyer Veterans Disability Lawyer
Are You Tired Of Walking Pad For Desk? 10 Inspirational Ideas To
Revive Your Love For Walking Pad For Desk under desk treadmill (Aileen)
What i do not understood is in truth how you’re not actually a lot
more smartly-appreciated than you may be now. You’re very intelligent.
You already know thus considerably on the subject of this topic, produced me personally consider
it from numerous various angles. Its like men and women are not involved until it’s one thing to accomplish with Girl gaga!
Your personal stuffs excellent. At all times maintain it up!
Your way of describing all in this piece of writing is actually pleasant,
every one can simply understand it, Thanks a lot https://chefthierry.ca/mousse-au-chocolat/
I am sure this article has touched all the internet visitors, its really really good piece of writing on building up new blog.
arieties and비아그라 구매 cultivars o비아그라 구매f ornamental비아그라 구매 cro
20 Things You Should Know About Certified Slots
Fair slots (https://www.metooo.Co.uk/)
AtenaLabs CBD is a leading provider of high-quality CBD products that are designed to help users experience the benefits of cannabis without the psychoactive effects of THC.
Look into my web-site … http://weddingmoa.com/bbs/board.php?bo_table=free&wr_id=692469
Unquestionably believe that which you said.
Your favorite justification appeared to be on the web the easiest thing to be aware of.
I say to you, I certainly get irked while people consider worries that they just do not know about.
You managed to hit the nail upon the top as well
as defined out the whole thing without having side-effects , people can take a signal.
Will likely be back to get more. Thanks
Why Online Shopping Stores In London Is Right For You Decorative Metal Bowl Stand
https://ketonaraacvketogummies.com are a unique combination of natural ingredients that are derived from apple cider vinegar. This product is designed to promote weight loss, improve digestion, and boost overall health.
The 10 Scariest Things About Blown Double Glazing Repairs Near Me double glazing Repairs near me
20 Things You Should Know About Modern Slots
slot machine features – Waldo
–
10 Tell-Tale Signs You Need To Get A New Online Famous
Shopping Sites Vimeo
I was recommended this web site by my cousin. I am not sure whether this post is written by
him as no one else know such detailed about my trouble. You’re incredible!
Thanks!
I believe that is one of the most significant info for me.
And i am happy studying your article. However wanna observation on few normal issues, The
web site taste is wonderful, the articles is really great : D.
Good job, cheers
Feel free to surf to my blog post … reviews for lung clear pro
Guide To Demo Slot Zeus Vs Hades Gratis: The Intermediate Guide In Demo Slot
Zeus Vs Hades Gratis Demo Slot Zeus Vs Hades Gratis (https://Dchanwoo.Com)
You should be a part of a contest for one of the greatest sites on the web.
I am going to recommend this blog!
What’s The Current Job Market For Upvc Window Handle Replacement Professionals Like?
upvc window handle replacement
Каждый год в середине сентября организовывается Тюменский инновационный форум «НЕФТЬГАЗТЭК».
Форум посвящен устройству мнтодов инноваторского роста секторов топливно-энергетического комплекса, рассмотрению а также изысканию заключений, созданию благоприятных условий для расчета инноваторских проектов. Ежегодный тюменский форум представляетсобой важной дискуссионной площадкой по развитию нефтегазовой сферы в России, содержит большой авторитет и своевременность, созвучен общей стратегии развития инноваторского курса в России
-https://neftgaztek.ru/
Why We Love Fela Case Settlements (And You Should Also!) Fela lawyers
aviator game download http://aviator-crash-game.ru .
10 Inspirational Images Of Sweet Bonanza Akun Demo trik
sweet bonanza gacor (epal.Kr)
Your Family Will Thank You For Having This Uk Online Shopping Sites For Mobile
Machine-Made Runner Rugs – Ahmad,
Guide To Double Glazing Near Me: The Intermediate Guide Towards Double Glazing Near
Me glazing
Hey there! This is my first visit to your blog!
We are a group of volunteers and starting a new
project in a community in the same niche. Your blog provided us useful information to work
on. You have done a extraordinary job!
Ежегодно в середине сентября проходит Тюменский инновационный форум «НЕФТЬГАЗТЭК».
Форум посвящен определению мнтодов инновационного роста секторов топливно-энергетического комплекса, рассмотрению а также определению заключений, созданию благоприятных условий для формирования инноваторских проектов. Ежегодный тюменский форум представляетсобой влиятельной дискуссионной площадкой по увеличению роста нефтегазовой ветви в Российской Федерации, содержит большой авторитет и своевременность, созвучен общей стратегии продвижения инновационного направления в Российской Федерации
-https://neftgaztek.ru/
Its like you read my mind! You appear to know
a lot about this, like you wrote the book in it or something.
I think that you can do with some pics to drive the message home a bit,
but other than that, this is excellent blog. A fantastic read.
I will certainly be back.
Meds prescribing information. Brand names.
cost cheap phenytoin prices
Best news about drugs. Read now.
aviator bonus game aviator-crash-game.ru .