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
sapporo88
sapporo88 login
sapporo88
Unquestionably believe that which you said.
Your favorite reason appeared to be on the internet the
easiest thing to be aware of. I say to you, I certainly get annoyed while people think about worries that they plainly do not know about.
You managed to hit the nail upon the top and defined out
the whole thing without having side effect , people could
take a signal. Will likely be back to get more. Thanks
Also visit my website: fitspresso reddit weight loss
Loan Consolidation Solutions For Individuals 대출 갈아타기
oyun siteleri slot: deneme veren slot siteleri – deneme bonusu veren slot siteleri
sapporo88 login
That includes a large-mini-fridge-sized battery, an energy meter and a large-backpack-sized inverter to convert the battery’s direct current (DC) back into alternating current (AC) that the house can use.
Question: How Much Do You Know About Audi Lost Key?
Audi Replacement Key Cost (O39Akk533B75Wnga.Kr)
Stand Strong Fencing
Nashville, Tennessee 37201, Unites Ѕtates
16154311511
Strong privacy fence options
en iyi slot siteleri 2024: casino slot siteleri – guvenilir slot siteleri
sapporo88
לבילוי בכרמל נערות ליווי בחיפה תמיד נמצאות כאן בשביל לפנק את הגברים ולעשות להם טוב. בין אם זה יום חורף קר או שיא הקיץ, סתיו הכל על מנת שתרגיש טוב. לבלות עם נערות ליווי בקריות חולם על בילוי מפנק ברמה גבוהה? אתה עומד לקבל את מה שאתה רוצה ועוד הרבה resource
Cannabidiol, commonly known as CBD, has surged in popularity
due to its potential therapeutic benefits.
Canna Plus CBD is one such product that seeks to offer these benefits through its unique formulation.
My spouse and I absolutely love your blog and find a lot of your post’s to be just what I’m looking
for. Would you offer guest writers to write content in your case?
I wouldn’t mind publishing a post or elaborating on a lot of the subjects you write related to here.
Again, awesome weblog!
en iyi slot siteleri: slot oyun siteleri – slot oyunlar? siteleri
sapporo88
yeni slot siteleri: yeni slot siteleri – en guvenilir slot siteleri
The ketogenic diet, commonly known as the keto diet, has gained significant popularity for its potential benefits in weight
loss, improved mental clarity, and enhanced energy
levels.
sapporo88 login
This is a topic that’s near to my heart… Thank you! Exactly where are your contact details though?
guvenilir slot siteleri 2024: slot oyunlar? siteleri – en iyi slot siteler
Really no matter if someone doesn’t understand after that its up to
other users that they will assist, so here it takes place.
Feel free to visit my blog post erec prime
deneme bonusu veren slot siteleri: yasal slot siteleri – slot siteleri guvenilir
Официальное получение диплома техникума с упрощенным обучением в Москве
Привет!
Заказать диплом любого ВУЗа.
telegra.ph/diplom-ohrannika-novogo-obrazca-kupit-08-13-9
If some one wants expert view regarding running a blog afterward i recommend him/her to
pay a quick visit this webpage, Keep up the nice job.
Great weblog here! Also your web site a lot up very fast!
What host are you the usage of? Can I get your affiliate
link for your host? I want my web site loaded up as quickly as
yours lol
Keep in mind, a referral letter is not the single evidence to verify your job remains in nationwide passion.
my web-site … EB2 Visa business plan (Greg)
https://denemebonusuverensiteler.win/# bahis siteleri