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
My brother recommended I might like this blog. He was entirely right.
This post actually made my day. You cann’t imagine simply how much time I had spent for
this info! Thanks!
%%
my blog: adhd assessment for adults london
%%
Here is my homepage – ghost immobiliser near Me (Fuchsia-Camel-fhb8B3.mystrikingly.Com)
asbestos lawsuit attorneys – proactive-orange-fp1g8b.mystrikingly.com,
%%
Here is my blog post; shop
%%
Here is my site: Rabbit Vibrator For Sale
%%
Also visit my website – replacement Jaguar key fob
Wow, amazing weblog structure! How long have you ever been running a
blog for? you made running a blog glance easy. The overall glance of your site is
wonderful, as neatly as the content material!
When someone writes an article he/she retains the image of a
user in his/her mind that how a user can understand it.
Therefore that’s why this article is perfect. Thanks!
%%
My website – service
mesothelioma asbestos law firm (class-action-lawsuit-asbe48715.Blog-mall.com)
windows And doors Bexley (https://funsilo.date)
If you take a long minute working to improve your content and you are able to have a high approval rate.
my web blog … https://claytonfsdnx.wikimillions.com/2764862/gsa_ser_link_building_mastery
%%
Here is my website :: jaguar x type key fob replacement (Lenora)
%%
Also visit my web blog: rabbit vibrators for sale (Andreas)
specializes in asbestos litigation (Kandis)
What’s up every one, here every one is sharing such know-how,
thus it’s fastidious to read this weblog, and I used to go to see this weblog every day.
upvc sash Windows Chesham
%%
My webpage – saab 9-3 replacement key (Daryl)
%%
Visit my homepage; uk representative (pa.biznet-us.com)
glazing kingston (Frances)
Whoa! This blog looks exactly like my old one!
It’s on a entirely different subject but it has pretty much the same layout and design. Great choice of
colors!
military asbestos lawsuit
If you’re looking to securely transact online, consider buying a verified Cash App account.
With verified accounts, you can confidently send and receive money, shop online, and even invest.
Enjoy added security and peace of mind with your financial transactions.
Don’t miss out on the convenience and safety of a genuine
Cash App account.
I just could not leave your site prior to suggesting that I extremely loved the standard information an individual provide to your guests?
Is going to be back steadily in order to check up on new posts
autowatch ghost Installer
Are you tired of facing restrictions on your Cash App account?
Buy a verified Cash App account and enjoy uninterrupted transactions.
Get instant access to all features and bid farewell
to account limitations. Don’t miss out on this opportunity – buy
your verified Cash App account today!
Hey There. I found your blog using msn. This is a
very well written article. I will be sure to bookmark it and come
back to read more of your useful information. Thanks for the post.
I will definitely comeback.
%%
Here is my blog: Cheap Double Glazing Milton Keynes (Telegra.Ph)
Are you in need of a verified Cash App account? Look no further!
Buying a verified Cash App account is the best solution for hassle-free online transactions.
With a verified account, you can enjoy increased transaction limits, secure payments, and peace
of mind. So why wait? Get your hands on a
verified Cash App account today and experience the convenience
of digital payments like never before!