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
10 Websites To Help You Become An Expert In Male Adult Toys adult Toys Shop
Wow! In the end I got a website from where I be capable of in fact get useful facts concerning my
study and knowledge.
It Is Also A Guide To ADHD Diagnosis Private In 2023 private adhd assessment somerset
Every weekend i used to pay a visit this website, as i want enjoyment, as this this web page conations really nice funny material too.
Five Single Mattresses Sale Lessons Learned From Professionals thick single Mattress
buy cheap ozempic buy cheap ozempic ozempic coupon
You’ll Never Be Able To Figure Out This Situstoto Slot’s Tricks situstoto Slot
http://rybelsus.shop/# buy semaglutide pills
https://rybelsus.shop/# rybelsus pill
I know this if off topic but I’m looking into starting my own blog
and was curious what all is needed to get setup? I’m assuming having a blog
like yours would cost a pretty penny? I’m not very internet smart so I’m not 100% positive.
Any suggestions or advice would be greatly appreciated. Thank you
https://ozempic.art/# ozempic generic
ozempic cost ozempic coupon buy cheap ozempic
11 Ways To Fully Defy Your Mental Health Assessment mental health act Assessment
15 Gifts For The Double Glazed Hinges Lover In Your Life pvc window hinges
В магазине сейфов предлагают сейф купить сейф в наличии
A Retrospective What People Said About Replacement Key For Audi 20 Years Ago Audi etron key (m.jingdexian.com)
That is very interesting, You are an excessively professional blogger.
I have joined your rss feed and look ahead to looking for extra of your fantastic
post. Additionally, I’ve shared your website in my social networks
This Week’s Most Popular Stories Concerning Metal Anal Butt Plug anal tops
Why Do So Many People Want To Know About Best Medication For Anxiety Disorder?Best Anxiety Disorder Treatment
https://ozempic.art/# buy cheap ozempic
What’s The Current Job Market For Wood Burning Fire Professionals?
wood burning fire (Floyd)
https://ozempic.art/# ozempic
How Foldable Wheelchair Ramp Propelled To The Top Trend In Social Media Foldable Ramp for wheelchair
5 Killer Quora Answers On Sliding Sash Windows sash Window restoration Near me
buy semaglutide pills semaglutide online rybelsus price
Greetings! This is my first visit to your blog! We are a team
of volunteers and starting a new initiative in a
community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!
Here is my blog post :: bclub.cm
What Do You Need To Know To Be Prepared To Portable Mobility Scooters For Sale heavy duty mobility scooters for sale near me
http://ozempic.art/# buy cheap ozempic
What Is Bandar Online Togel? How To Utilize It bandar togel terpercaya (weinreich-link-3.blogbright.Net)
This web site certainly has all of the info I wanted about this subject and didn?t know who to ask.
my website; https://thanhdatmedical.vn/may-xquang-nha-khoa/