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
Ballet Bar For Ballerinas – Differing Types Used For Many Different
Performances 유성구오피
https://denemebonusuverensiteler.win/# bahis siteleri
See What Green Sleeper Sofa Tricks The Celebs Are Using green sleeper sofa; https://mail.robertchang.ca/,
20 Trailblazers Are Leading The Way In Electric Wall Fireplace electric fire for wall (altodev.Ansanbaedal.shop)
What Is Severe Anxiety Disorder Symptoms And How To Use What Is Severe Anxiety Disorder Symptoms And How To Use liver Anxiety symptoms
The Most Effective Reasons For People To Succeed In The Mesothelioma
Attorneys Industry Mesothelioma Legal; Fourtoons.Com,
Hiya! I know this is kinda off topic however I’d figured I’d ask.
Would you be interested in trading links or maybe guest authoring a
blog post or vice-versa? My blog covers a lot
of the same topics as yours and I think we could greatly benefit from each other.
If you’re interested feel free to shoot me an email. I look forward to hearing from you!
Awesome blog by the way!
en guvenilir slot siteleri: deneme veren slot siteleri – slot bahis siteleri
смотреть
Wow, this paragraph is pleasant, my younger sister is analyzing
these things, thus I am going to let know her.
bonus veren slot siteleri: 2024 en iyi slot siteleri – slot siteleri bonus veren
5 Myths About Espresso Machines That You Should Avoid Espressomachines (https://Utahsyardsale.Com/)
Meds information leaflet. Drug Class.
can i purchase irbesartan pill
Actual news about pills. Get now.
Массаж Ростов
Indisputable Proof You Need Cuisinart Coffee Maker 822547
в этом разделе представлены только известные, надежные и лучшие покерные школы, https://https://kzpokerdompro.com// а также обзоры на них.
See What Treadmill For Sale Near Me Tricks The Celebs Are Utilizing treadmill for sale near me
How To Tell If You’re Prepared To Go After Situstoto Slot Togel hongkong
en iyi slot siteleri 2024: slot casino siteleri – yasal slot siteleri
You’ll Never Guess This Double Single Bunk Bed’s Tricks double single bunk
Visit my web site: Daycare Near Me
Very good info. Lucky me I ran across your blog by chance (stumbleupon).
I have book marked it for later!
Daftar Situs Togel: The Good, The Bad, And The Ugly togel Hongkong
Guide To Cream Electric Stove Fire: The Intermediate Guide The Steps To
Cream Electric Stove Fire Cream electric stove fire
http://denemebonusuverensiteler.win/# deneme bonusu
casino slot siteleri: en iyi slot siteleri 2024 – en cok kazandiran slot siteleri
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 can do with a few pics to
drive the message home a little bit, but instead of that, this is fantastic blog.
An excellent read. I’ll certainly be back.
What’s The Current Job Market For Single Stroller Cheap Professionals Like?
single stroller cheap – http://www.arkmusic.co.kr/ –
How To Explain Mental Illness Assessment To A Five-Year-Old mental status assessment
Fantastic forum posts, Thanks a lot!