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
Excellent post. I was checking constantly this weblog
and I am inspired! Very useful info specially the remaining section :
) I take care of such info a lot. I was seeking this certain info for a very long time.
Thanks and best of luck.
Here is my web page :: is lung clear pro legit
buy cheap ozempic: buy ozempic pills online – buy ozempic
Why You’ll Definitely Want To Find Out More About Car Diagnostic Near Me engine diagnostics
[Rozella]
Ozempic without insurance: Ozempic without insurance – buy ozempic pills online
I am actually thankful to the holder of this site who has shared this fantastic article at at this time.
My page: Inviertas
The Unknown Benefits Of Pragmatic 프라그마틱
betting sign up promos: A code that unlocks a free bet on a betting site, allowing users to place a wager without using their own funds, often as part of a promotion.
The Most Common Mistakes People Make Using Fireplace Surround wood fireplaces
https://ozempic.art/# buy ozempic pills online
Сервисный центр предлагает выездной ремонт холодильников indesit центр ремонта холодильника indesit
Magnificent beat ! I wish to apprentice at the same time as you amend your web site, how could i subscribe for a blog site?
The account aided me a applicable deal. I had been a little bit
familiar of this your broadcast provided vibrant transparent concept
Superb postings, Kudos!
https://pacman168vip.com/ยกขบวนเกมสล็อตยอดฮิตจากค่ายดังต่างประเทศ
buy ozempic pills online ozempic buy ozempic pills online
https://ozempic.art/# buy ozempic
https://rybelsus.shop/# rybelsus pill
Good article! We are linking to this particularly great content on our
website. Keep up the great writing.
Also visit my blog: megaweb at
semaglutide online: semaglutide online – buy semaglutide pills
An intriguing discussion is definitely worth comment.
I do believe that you need to publish more on this subject matter, it may not be a
taboo subject but usually folks don’t discuss such issues.
To the next! Kind regards!!
semaglutide online: rybelsus coupon – rybelsus price
ozempic buy ozempic ozempic generic
Профессиональный сервисный центр по ремонту электросамокатов в Москве.
Мы предлагаем: где починить электросамокат в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Bean To Cup Coffee Makers Explained In Less Than 140
Characters Bean To Cup Or Espresso Machine
20 Pragmatic Ranking Websites Taking The Internet By Storm 프라그마틱 공식홈페이지
Hey! Someone in my Facebook group shared this website with us so I came to take a look.
I’m definitely enjoying the information. I’m bookmarking and will be tweeting this to my followers!
Fantastic blog and excellent design.
When I initially commented I clicked the “Notify me when new comments are added”
checkbox and now each time a comment is added I get four emails with the same comment.
Is there any way you can remove me from that
service? Bless you!
semaglutide cost: rybelsus pill – rybelsus price
I’m not that much of a internet reader to be honest but
your sites really nice, keep it up! I’ll go ahead and bookmark your site
to come back later. Cheers
https://ozempic.art/# ozempic generic
https://rybelsus.shop/# rybelsus cost