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 Mobile Apps That Are The Best For Commercial Truck
Accident Attorney truck wreck Attorney (https://www.metooo.es/)
I do agree with all the concepts you have introduced to your post.
They’re really convincing and can certainly work.
Nonetheless, the posts are very brief for beginners. May just you please prolong them a little from subsequent time?
Thank you for the post.
How To Backup Microsoft Windows Xp Home Edition 에볼루션 도메인
10 Facts About Accident Lawyer Brooklyn That Will Instantly Set
You In A Positive Mood injury lawsuit (Therese)
Night Spa hiop
Stress Management And Rush Hour Traffic 하이오피사이트
Informative article, exactly what I wanted to find.
This was highly educational. More at متن روز پدر برای استوری
New Tips For Small Business Holiday Marketing 하이오피주소
Many sites offer a good price of mesalamine immunosuppressant s online can be both safe and risky.
Well explained. Discover more at click here
Article writing is also a fun, if you be acquainted with
after that you can write otherwise it is difficult to write.
My friend recommended Mt Roof Moss Removal
포천에도 돈 벌 곳은 있습니다. 포천 노래방알바로 연락 한 번 주세요
Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell
to her ear and screamed. There was a hermit crab inside and it pinched
her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!
https://sweetbonanza25.com/# sweet bonanza demo oyna
Многие пользователи предпочитают зеркала основного сайта Starda Casino, так как они обеспечивают более стабильный доступ к сети казино.
At this moment I am ready to do my breakfast, after having my breakfast coming again to read additional news.
Thanks for the valuable insights. More at kitchen remodeling company
http://casinositeleri25.com/# guvenilir casino siteleri
guvenilir casino siteleri: canl? casino siteleri – Casino Siteleri
요즘 가락쪽 침체기라고 하지만, 저희는 다릅니다. 가락동 노래방알바에서 확인 해보세요.
sweet bonanza oyna: sweet bonanza slot – sweet bonanza giris
sweet bonanza yorumlar sweet bonanza slot sweet bonanza oyna
deneme bonusu veren siteler: deneme bonusu veren siteler – deneme bonusu veren yeni siteler
9 Things Your Parents Taught You About Darling Hahns Macaw Darling hahns macaw (Laura)
Top Three Healthiest And Tastiest Protein Bars 오피 (Kristi)
Good answers in return of this query with solid arguments and describing
all concerning that.
This was quite helpful. For more, visit برگرداندن پست حذف شده اینستاگرام
http://slotsiteleri25.com/# slot oyunlar?