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
What’s up, I desire to subscribe for this blog to take hottest updates, thus where can i do it please assist.
Fridge Freezer With Ice And Water Dispenser: What No One Is
Talking About refrigerator water cooler (Nilda)
Why Getting An ADHD Diagnosis Is Fast Becoming The Most Popular
Trend In 2023 Adult Adhd Diagnosis northern ireland
10 Things You Learned In Kindergarden To Help You Get
Started With Mesothelioma Attorney Mesothelioma Claim
The Reason Treadmill Foldable Electric Is So Beneficial
When COVID-19 Is In Session Folding Electric Treadmill Uk
Ten Double Pushchairs That Really Change Your Life Double pushchairs
See What Experienced Birth Injury Lawyers Tricks The Celebs Are Using Experienced Birth Injury Lawyers
en iyi slot siteleri: slot oyunlar? siteleri – slot siteleri bonus veren
Please let me know if you’re looking for a article author for your site.
You have some really good articles and I believe I would
be a good asset. If you ever want to take some
of the load off, I’d absolutely love to write some material for your blog in exchange
for a link back to mine. Please shoot me an email if interested.
Many thanks!
Highly descriptive post, I loved that a lot. Will there be a
part 2?
bonus veren slot siteleri: en cok kazandiran slot siteleri – slot bahis siteleri
How You Can Use A Weekly Electric Fireplace Wall Mounted
Project Can Change Your Life modern fireplace
Great blog here! Also your website loads up fast!
What host are you using? Can I get your affiliate link
to your host? I wish my website loaded up as fast as yours lol
It’s a pity you don’t have a donate button! I’d without a doubt donate to this fantastic blog!
I suppose for now i’ll settle for book-marking and adding your
RSS feed to my Google account. I look forward to fresh updates and will talk about this site with my Facebook group.
Chat soon!
You’ll Be Unable To Guess Treadmill Incline Workout’s Tricks
Treadmill incline workout
slot siteleri 2024: guvenilir slot siteleri 2024 – bonus veren slot siteleri
Nice blog here! Also your web site loads up fast! What
host are you using? Can I get your affiliate link
to your host? I wish my web site loaded up as fast as yours lol
You’ll Never Be Able To Figure Out This Couch L Shaped Sofa’s Secrets Couch l shaped Sofa
What’s The Job Market For Treadmill Shop Near Me Professionals Like?
Treadmill Shop Near Me
slot oyunlar? siteleri: slot siteleri guvenilir – slot siteleri 2024
Thanks , I’ve recently been searching for information approximately this topic for a while and yours is the best I’ve came
upon till now. However, what concerning the conclusion? Are you positive in regards to the supply?
en iyi slot siteler: slot oyunlar? siteleri – en guvenilir slot siteleri
15 Funny People Who Are Secretly Working In Washington Mesothelioma Lawyer Mesothelioma lawyer ca
I’m not that much of a internet reader to be honest but your blogs
really nice, keep it up! I’ll go ahead and bookmark your site to
come back later. Cheers
5 Killer Qora’s Answers To Volvo C30 Key volvo v40 key programming
deneme bonusu veren slot siteleri: slot oyun siteleri – slot bahis siteleri
deneme bonusu veren slot siteleri: yeni slot siteleri – casino slot siteleri
fantastic publish, very informative. I ponder why the opposite
experts of this sector don’t realize this.
You should proceed your writing. I’m sure, you’ve a huge readers’ base already!
Do You Think You’re Suited For Doing Volvo Keyfob?
Take This Quiz remote
Pay Attention: Watch Out For How ADHD Diagnosis Private UK
Is Taking Over And How To Stop It private adhd Assessment Ipswich