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
Hi mates, its great piece of writing about tutoringand entirely defined, keep it up all the time.
%%
My web page … Cs go battle case (bbs.dubu.com.cn)
If you’re looking to securely buy and sell goods or services online,
consider investing in a verified Cash App account. With its robust
security features and authentication process, you can have peace of mind while conducting transactions.
Don’t compromise your safety, opt for a verified Cash App account today.
An intriguing discussion is worth comment. I do believe that you ought to publish more about this topic, it might not be a taboo subject but usually people don’t speak about these issues.
To the next! Cheers!!
Buy a verified Cash App account and stay worry-free about payment
transactions. With a verified account, you can securely send and receive
money, make online purchases, and enjoy various Cash App features hassle-free.
Don’t wait, get yourself a verified Cash App account today and experience seamless money management.
Nice post. I learn something totally new and challenging
on blogs I stumbleupon everyday. It’s always helpful to
read content from other authors and practice
a little something from their websites.
Buying a verified Cash App account can be a convenient and trustworthy option for those seeking to make secure
and hassle-free online transactions. With a verified account, users can enjoy benefits like increased transaction limits, easy withdrawal
options, and enhanced security measures. By purchasing a verified Cash App account, users can ensure a smooth and reliable payment experience,
making their online transactions a breeze.
If you’re looking to buy a verified Cash App account, look no further!
A verified account allows you to enjoy all the
benefits of this popular mobile payment app, providing a secure and convenient
way to send and receive money. With a verified account, you can enjoy higher transaction limits and added security measures.
Don’t wait, get your verified Cash App account today!
Thanks designed for sharing such a nice thought,
paragraph is nice, thats why i have read it entirely
I got this web site from my buddy who shared with
me about this web site and at the moment this time I am browsing this website and reading very informative content here.
A verified Cash App account ensures a safe and secure
experience while managing your finances. By purchasing a verified account, you gain access to added features and benefits such
as higher spending limits and the ability to send and receive money with confidence.
Don’t compromise your financial security, invest in a verified Cash App account today.
Looking to buy verified Cash App accounts? Look no further!
Buying a verified Cash App account can provide
you with added security and peace of mind. With a verified
account, you can send and receive money without any limitations.
Don’t miss out on this opportunity, purchase your verified
Cash App account today!
Piece of writing writing is also a excitement, if you be familiar with afterward you can write if not it is difficult to write.
%%
Stop by my web blog: double glazing in aldershot – Margarette –
%%
My web blog: organic Cbd oil
%%
Here is my blog post; online part time work From home
10 . Pinterest Account bean to cup coffee machine with automatic milk frother (Stepanie)
Be Following About Coffee Machine Beans
As the admin of this web site is working, no question very soon it will be well-known, due to its feature contents.
%%
Here is my web-site – installers
specializes in asbestos litigation (Luz)
%%
my website :: replacement honda keys
window fitters berkhamsted
%%
Here is my site; representative
upvc window repairs hitchin (Joseph)
alabama mesothelioma Attorney
I am regular reader, how are you everybody? This piece of
writing posted at this site is in fact fastidious.
%%
Feel free to surf to my web-site … automotive locksmith cost
(Mattie)
alabama mesothelioma attorney (Daniel)
Sokolove law Asbestos
%%
Look at my page – csgo case opening websites (milkyway.cs.rpi.edu)