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
пин ап: пин ап зеркало – пин ап вход
https://pin-up.diy/# pin up
We stumbled over here by a different page and thought I might
check things out. I like what I see so now i am following you.
Look forward to looking at your web page repeatedly.
1хбет зеркало 1xbet официальный сайт 1xbet зеркало рабочее на сегодня
Awesome blog! Do you have any helpful hints for aspiring
writers? I’m planning to start my own blog soon but I’m a little lost on everything.
Would you advise starting with a free platform like
Wordpress or go for a paid option? There are so many choices out there that I’m completely overwhelmed ..
Any ideas? Thank you!
пин ап зеркало: пин ап казино вход – пин ап зеркало
Appreciate the recommendation. Let me try it out.
In the fast-evolving world of nutritional supplements, Sweet Restore Glycogen Support
Supplement has garnered attention for its promise of bolstering energy reserves and aiding recovery,
especially for athletes and fitness enthusiasts.
пин ап казино: пин ап казино вход – пин ап зеркало
amazing very good
http://vavada.auction/# vavada зеркало
pin up казино: пин ап казино – пин ап казино вход
Greetings! Quick question that’s completely off topic. Do you know how to make your
site mobile friendly? My blog looks weird when viewing from my iphone4.
I’m trying to find a template or plugin that might be able to fix this problem.
If you have any recommendations, please share.
With thanks!
Hi, yeah this post is actually nice and I have learned lot of things from
it concerning blogging. thanks.
https://vavada.auction/# вавада казино
pin up казино: пин ап зеркало – пин ап
1xbet скачать: 1xbet зеркало рабочее на сегодня – 1xbet официальный сайт мобильная версия
1вин сайт: 1вин сайт – 1win вход
Please let me know if you’re looking for a writer for your site.
You have some really good posts and I feel I would be a good asset.
If you ever want to take some of the load off, I’d love to write
some articles for your blog in exchange for a link back to mine.
Please send me an e-mail if interested. Many thanks!
Thank you for delivering top-notch web creation together with
WordPress care.
pin up: пинап казино – пинап казино
There is noticeably a bundle to identify about this.
I assume you made some good points in features also.
Feel free to surf to my page – Pinterest
Hello there! This article could not be written any better!
Going through this post reminds me of my previous roommate!
He constantly kept preaching about this.
I most certainly will forward this article to
him. Fairly certain he’ll have a very good read.
Thanks for sharing!
пин ап зеркало: пин ап казино вход – пин ап вход
신규 하이카지노 플러스카지노 후속 카지노사이트 추천드리고 있습니다. 하이카지노
пин ап казино вход: пинап казино – пин ап вход
Greate article. Κeep posting ѕuch kind of іnformation on your site.
Im reɑlly impressed by yοur site.
Hello tһere, You’ve done а great job. Ӏ’ll ⅾefinitely digg
iit ɑnd personally suggеst tօ my friends.
Ӏ am sᥙгe they will be benefited from this
site.
Alsso visit mʏ blog post: akun slot gacor
Hi, i read your blog from time to time and i own a similar one and i was just wondering if
you get a lot of spam comments? If so how do you protect
against it, any plugin or anything you can suggest?
I get so much lately it’s driving me insane so any
help is very much appreciated.
Here is my page; herpesyl
http://vavada.auction/# vavada
What i do not realize is in fact how you’re no longer really
much more neatly-favored than you might be now. You are very
intelligent. You already know therefore significantly
in the case of this subject, produced me in my opinion consider it from a lot of numerous angles.
Its like women and men aren’t fascinated unless it is one thing to accomplish with Girl gaga!
Your individual stuffs great. All the time take care of it up!