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
I know this if off topic but I’m looking into starting my own weblog and was wondering what all is required
to get set up? I’m assuming having a blog like yours would cost a pretty penny?
I’m not very internet savvy so I’m not 100% sure. Any tips or advice would be greatly
appreciated. Thank you
Namun karena kebijakannya, aplikasi ini tidak memperbolehkan orang-orang untuk mengunduh video
tersebut karena melanggar hak cipta dan privasi.
11 “Faux Pas” That Are Actually Okay To Make With Your Lightest Folding Electric Wheelchair folding power wheelchair
Five Killer Quora Answers On Daftar Situs Togel daftar situs togel (bookmarking.win)
The Most Underrated Companies To Follow In The Vibrator Clit Industry Best Clitoral Vibrator (http://Molchanovonews.Ru/User/Rateattack23/)
https://ozempic.art/# buy cheap ozempic
Write more, thats all I have to say. Literally, it seems
as though you relied on the video to make your point. You obviously
know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something enlightening
to read?
https://rybelsus.shop/# rybelsus coupon
10 Places Where You Can Find Mobile Car Diagnostics Near Me Uk jobs
Hello I am so glad I found your site, I really found
you by error, while I was searching on Digg for something else, Anyhow I am here now and would
just like to say thank you for a marvelous post and
a all round enjoyable blog (I also love the theme/design), I don’t have
time to browse it all at the minute but I have bookmarked it and also included your RSS feeds, so when I have
time I will be back to read a great deal more, Please do keep up the great jo.
I quite like looking through an article that will make men and women think.
Also, thanks for allowing for me to comment!
Best Automatic Folding Mobility Scooter Uk Tools To Improve Your Everyday Lifethe Only Best Automatic Folding Mobility Scooter Uk Technique Every Person Needs To Learn Automatic Folding mobility Scooter Uk
The 10 Most Scariest Things About Sofas L Shape sofas l shape
Guide To Patio Sliding Doors Repair: The Intermediate Guide
On Patio Sliding Doors Repair patio sliding Doors repair
Best Auto Folding Mobility Scooter Uk’s History History Of Best Auto
Folding Mobility Scooter Uk auto folding mobility scooters (Florida)
What The Heck What Exactly Is Treatments Of ADHD?Adhd Treatment For Adults Online
https://rybelsus.shop/# buy semaglutide online
Gold Partnership is sell a business – Terese,
relied on choice worth considering for those new to the Gold individual retirement account
market.
The Best 2 In 1 Car Seat Stroller Tricks For Changing Your Life best 2 in 1 Car seat Stroller
3 Ways That The 5kw Wood Burning Stove Will Influence Your Life small wood burning stove for shed uk [Daniel]
The Most Significant Issue With Electric Wall Fireplace And How To Fix It hang on wall fireplace (Tammy)
Appreciate the recommendation. Will try it
out.
20 Myths About Private Diagnosis Of ADHD: Busted Private Adhd Assessment Kent
I like the valuable information you provide in your articles.
I will bookmark your weblog and check again here regularly.
I am quite certain I will learn lots of new stuff right here!
Good luck for the next!
Also visit my webpage :: defender sugar
Window Repairs Tips To Relax Your Daily Life Window Repairs Trick
That Every Person Should Know Window Repairs
There is certainly a great deal to find out about this subject.
I like all the points you’ve made.
Wonderful article! We are linking to this particularly great post on our website.
Keep up the great writing.
wonderful post, very informative. I wonder why the opposite experts of
this sector don’t notice this. You must continue your
writing. I am confident, you’ve a great readers’ base already!
Профессиональный сервисный центр по ремонту электросамокатов в Москве.
Мы предлагаем: ремонт контроллера самоката
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Профессиональный сервисный центр профессиональный ремонт телефонов отремонтировать телефон