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
14 Questions You’re Anxious To Ask Best Brands For Refrigerators Refrigeration Brands
here https://glassartistsfellowship.com/2024/06/13/keberuntungan-dalam-soju88-slot-petualangan-di-dunia-permainan-kasino/
Hello there! I know this is kinda off topic but I was wondering which blog platform are you using
for this website? I’m getting fed up of WordPress
because I’ve had issues with hackers and I’m looking at alternatives for another
platform. I would be awesome if you could point
me in the direction of a good platform.
yat?r?ms?z deneme bonusu veren siteler deneme bonusu veren siteler yeni deneme bonusu veren siteler yeni
https://denemebonusuverensiteler25.com/# deneme bonusu veren yeni siteler
guvenilir casino siteleri: canl? casino siteleri – canlД± casino bonusu veren siteler
Appreciate the insightful article. Find more at عمل پی ار کی
일상이 지겨우신가요? 신선함을 느끼고 싶다면 평택호빠로 놀러오세요
https://slotsiteleri25.com/# guvenilir slot siteleri
Flood recovery requires specialized skills—get professional help from water damage cleanup if you’re affected
10 Expressions To Avoid In Sales Communication 에볼루션 베팅취소
deneme bonusu veren casino siteleri guvenilir casino siteleri deneme bonusu veren casino siteleri
sweet bonanza giris: sweet bonanza guncel – sweet bonanza demo oyna
10 Top Facebook Pages Of All Time About Truck Accident Attorneys
lawyer truck Accident – https://www.metooo.io,
Burlesque Show 하이오피주소
make stamps online make stamps online .
I have been exploring for a bit for any high-quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this info So i?m happy to convey that I’ve a very good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to do not forget this web site and give it a look regularly.
https://denemebonusuverensiteler25.com/# yat?r?ms?z deneme bonusu veren siteler
Casino Siteleri: casino bahis siteleri – Casino Siteleri
In some scenarios, it might be best to place pet dogs in a
little bedroom and close the door.
Fixed-income funds are made up of government and corporate securities that provide a fixed return and are usually low risk.
I absolutely love your blog and find the majority of your post’s to be just what I’m looking
for. can you offer guest writers to write content
for you? I wouldn’t mind producing a post or elaborating on a
lot of the subjects you write with regards to here.
Again, awesome website!
Good response in return of this matter with real arguments and describing all regarding that.
The goal is to locate commonalities and get to
an arrangement without going to court.
You actually make it seem so easy with your presentation but I find this topic to be really something that I think I
would never understand. It seems too complex
and very broad for me. I’m looking forward for your next
post, I’ll try to get the hang of it!
Быстрое seo продвижение сайтов в google
You’ll Be Unable To Guess Patio Doors Repair Near Me’s Secrets patio doors repair near me (trade-britanica.trade)
tor dark web onion market dark markets
This was a fantastic read. Check out queen and slim Mural for more
free stamp maker online http://www.stamp-creator-online0.com/ .