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
gomelsutochno.ru
Профессиональный сервисный центр ремонт смартфонов рядом ремонт сотовых телефонов рядом
XYGENYX, a licensing company for FDA-registered items.
Here is my web page; Bookmarks
I am sure this post has touched all the internet visitors, its really really good paragraph on building up new blog.
My page … Bookmarks
Hello very nice site!! Man .. Beautiful .. Amazing ..
I will bookmark your blog and take the feeds additionally?I am glad to search out so many useful information here within the publish, we’d like develop extra techniques on this regard, thanks
for sharing.
Feel free to surf to my webpage – newborn baby
5 Biofuel For Fireplace Lessons Learned From The Pros ethanol fireplace burner – Fernando,
Hi, I wish for to subscribe for this blog to take most recent updates, therefore where can i do it
please help.
Here is my web-site sight care
The intelligence is the most effective dry herb vape
I have tried.
Here is my web blog … Bookmarks
Зеркала Pinco Casino регулярно обновляются
и обеспечивают тот же уровень безопасности и надежности, что и основной сайт.
Some individuals vegetable based vape juice flavorful pure nicotine
semaglutide tablets rybelsus cost buy semaglutide online
I’m very pleased to discover this page. I wanted to thank you for
your time due to this wonderful read!! I
definitely loved every part of it and I have you bookmarked to see new things on your website.
My web page: baby care information
Deference to op, some fantastic selective information.
Here is my web site: daycare business.start (Chelsey)
9 . What Your Parents Taught You About All Terrain Rollator Walker With Seat all terrain rollator Walker with seat
Hiya, I am really glad I have found this information. Nowadays bloggers
publish only about gossips and internet and this is really irritating.
A good blog with exciting content, that is what I need.
Thank you for keeping this site, I’ll be visiting it.
Do you do newsletters? Cant find it.
Also visit my homepage :: reliable child care – George –
Very quickly this website will be famous among
all blog users, due to it’s good content
Where Are You Going To Find Heavy Duty Electric Wheelchair Be 1 Year From Now?
all terrain electric wheelchair uk (nearestmobilityshop43221.atualblog.com)
NewULife Ranks. Welcome to my NewUlife Review.
Feel free to surf to my site; Bookmarks
A Relevant Rant About Daftar Akun Togel Resmi togel hongkong; Savannah,
XYGENYX, a licensing firm for FDA-registered items.
My web page: functional mushroom gummies
http://rybelsus.shop/# rybelsus cost
XYGENYX, a licensing business for FDA-registered products.
my blog: cbd muscle balm reviews
Appreciate it for helping out, excellent info.
Stop by my web page day care homes
No business is too big or small for Quick Electricity.
Feel free to surf to my homepage Bookmarks
In this case, the life insurance business’s money.
Here is my web blog … euphoria mushroom gummies
Try to find exactly how the law office responds to those reviews.
my webpage :: Bookmarks
http://ozempic.art/# buy ozempic pills online
Some individuals vape flavorful pure nicotine
my webpage … where can you buy plant based foods
XYGENYX, a licensing firm for FDA-registered items.
Feel free to surf to my homepage; functional mushroom gummies
Keep on writing, great job!
Also visit my website – childcare workers