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
Very shortly this web page will be famous amid all blogging people, due
to it’s nice content
Stop by my web blog: koitoto togel
7 Simple Secrets To Completely You Into Window Glass Repairs
repairing glass (Raquel)
Five Killer Quora Answers On Link Togel Resmi link Togel resmi
The Best Pragmatic Free Trial Meta Techniques For Changing Your Life 프라그마틱 무료스핀 (Warren)
В магазине сейфов предлагают где купить сейфы в москве сейфы простые
Admiring the persistence you put into your website and in depth
information you provide. It’s nice to come across a blog every once in a while
that isn’t the same old rehashed information. Wonderful read!
I’ve saved your site and I’m adding your RSS feeds to my Google account.
11 “Faux Pas” You’re Actually Able To Create With Your Wall.Mounted Fireplace fireplaces on wall (Ruby)
http://rybelsus.shop/# buy rybelsus online
rybelsus price semaglutide online buy rybelsus online
https://rybelsus.shop/# rybelsus price
You can definitely see your expertise in the article you write.
The world hopes for even more passionate writers such as you who are not afraid to mention how they believe.
All the time follow your heart.
The 10 Most Popular Pinterest Profiles To Keep Track Of Best Kids’
Bunk Beds bunk beds for kids – cplsportst11.Com,
Nine Things That Your Parent Taught You About Audi Keys audi car key case
The 10 Most Terrifying Things About Best Bunk Bed Design Best Bunk bed Design
Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
I think that you could do with some pics to drive the message home a bit, but instead
of that, this is great blog. An excellent read. I’ll definitely be back.
Thanks for the auspicious writeup. It in truth was once
a entertainment account it. Look advanced to more introduced agreeable from you!
By the way, how can we communicate?
10 Websites To Help You To Become An Expert In Upvc Window Handle Replacement Double Glazing Window Handle
buy ozempic: buy ozempic pills online – ozempic cost
I do trust all of the ideas you’ve offered for your post.
They are really convincing and can certainly work. Nonetheless, the posts are very brief for newbies.
Could you please prolong them a bit from next time?
Thanks for the post.
5 Common Phrases About Electric Fireplace Wall Mounted You Should Avoid slimline wall mounted electric fires, Claire,
В магазине сейфов предлагают сейфы сейфы
Для своих Психолог сейчас
https://rybelsus.shop/# rybelsus pill
Профессиональный сервисный центр по ремонту планшетов в Москве.
Мы предлагаем: ремонт планшетов на дому
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
https://ozempic.art/# ozempic generic
Свой Психолог сейчас
Wonderful work! This is the type of information that are supposed to be shared around
the net. Disgrace on the seek engines for now not positioning this put up higher!
Come on over and discuss with my site . Thank you =)
Свой Психолог сейчас
The Best Sports Toto Korea It’s What Gurus Do Three Things 첫가입 꽁머니
http://ozempic.art/# Ozempic without insurance