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
I pay a visit daily a few blogs and information sites
to read content, however this weblog offers feature
based writing.
clomid tablets how to get clomid price cost of clomid now
http://free-minigames.com/application/list/?megapari_promokod_besplatno.html
deep web sites darkmarket darknet websites
Hi there everyone, it’s my first pay a quick visit at this web site, and paragraph is really fruitful in support of me,
keep up posting these content.
Интересные статьи о займах – https://telegraf.by/press-relizi/67/na-chto-stoit-obratit-vnimanie-pri-oformlenii-zajma/
If some one needs expert view on the topic of blogging then i propose him/her to pay a quick visit this
web site, Keep up the pleasant work.
This blog has some excellent advice for optimizing landing pages! More insights can be found at rockland seo
Buzzwords, De-Buzzed: 10 Other Methods To Say Buy
Real Driving License Experiences Deutschen Registrierten FüHrerschein Kaufen
What are some top tips from professionals like those at #yourkeyword# for DIY decorations? christmas light installation near me
The 10 Most Scariest Things About Crypto Local Casino crypto local casino
(Curtis)
Good day! Do you use Twitter? I’d like to follow you if that would be okay.
I’m absolutely enjoying your blog and look forward to new posts.
I love what you guys are usually up too. This kind of clever work and
exposure! Keep up the awesome works guys I’ve incorporated you guys to my own blogroll.
On our website you will find unique collection rare coins from the whole planet. We offer both modern and historical specimens that will become real finds for collectors.
For more information, follow the link [url=https://numizmatexpert.ru/]монета ежик в тумане 2024 [/url]
Our team of experts carefully selects each coin, ensuring high quality.
My spouse and I stumbled over here different web page
and thought I might as well check things out.
I like what I see so now i am following you.
Look forward to looking into your web page yet again.
Процесс получения диплома стоматолога: реально ли это сделать быстро?
amoxicillin without rx 875 mg amoxicillin cost amoxicillin medicine
It’s difficult to find knowledgeable people about this topic, but you sound like
you know what you’re talking about! Thanks
I learned so much about how to choose the right #moving company# through online Cleveland apartment movers
I like what you guys are usually up too. This kind of clever work and
coverage! Keep up the very good works guys I’ve added
you guys to our blogroll.
The Reasons Why Adding A Pragmatic To Your Life Will Make All The A Difference 프라그마틱 무료 슬롯버프 – Walter –
hello there and thank you for your info – I’ve definitely
picked up something new from right here. I did however expertise a few technical points using this site, since I experienced to reload the
website a lot of times previous to I could get
it to load properly. I had been wondering if your hosting is
OK? Not that I am complaining, but sluggish loading instances times will sometimes affect your placement in google and
could damage your high-quality score if advertising and marketing with Adwords.
Anyway I’m adding this RSS to my email and can look out for much more of your respective
intriguing content. Ensure that you update this again very soon.
http://cipharmdelivery.com/# cipro ciprofloxacin
amoxicillin generic cost of amoxicillin prescription medicine amoxicillin 500
Быстрая схема покупки диплома старого образца: что важно знать?
спб нарколог вывод из запоя спб нарколог вывод из запоя .
https://clomidonpharm.com/# where can i get clomid prices
Интересные статьи о займах – https://dubna.ru/article/2022/04/qiwi-koshelek-teper-eshchyo-i-bystrye-zaymy-na-kartu
I’m impressed, I have to admit. Rarely do I come across a blog that’s both educative and
amusing, and without a doubt, you’ve hit the nail on the head.
The problem is an issue that not enough men and women are speaking intelligently about.
I am very happy I found this during my hunt for something relating to this.
Here’s hoping everyone gets their chance on the water soon; we need more surfers in our world according to # # anyKeword fulcrum surf competition