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
http://sildenafiliq.com/# buy viagra here
Pretty great post. I simply stumbled upon your weblog and wished to
say that I have truly enjoyed browsing your blog
posts. After all I’ll be subscribing in your rss feed and
I am hoping you write once more soon!
cheap kamagra: super kamagra – Kamagra 100mg
Very energetic blog, I liked that a lot. Will
there be a part 2?
http://sildenafiliq.xyz/# Viagra online price
Drugs information leaflet. Short-Term Effects.
avodart
Best news about medicine. Read here.
https://yogaufa.ru
prayertimein.com
buy Viagra online cheapest viagra Generic Viagra online
для наиболее продвинутых игроков доступны столы с в достаточной степени proplayredstarpoker.ru высокими лимитами $50/$100.
Also visit my blog post … https://proplayredstarpoker.ru/
Generic Viagra for sale buy viagra online buy Viagra online
buy viagra here buy viagra online Viagra without a doctor prescription Canada
Generic Viagra for sale generic ed pills cheap viagra
Cialis without a doctor prescription: cheapest cialis – Buy Tadalafil 20mg
sildenafil online: sildenafil iq – Viagra Tablet price
http://sildenafiliq.com/# Cheap Viagra 100mg
https://sildenafiliq.com/# Generic Viagra for sale
Drug information. What side effects?
proscar
Some news about medicine. Read here.
Excellent blog here! Also your site 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
https://sildenafiliq.xyz/# buy Viagra over the counter
http://kamagraiq.com/# п»їkamagra
https://tuapseteatr.ru/
Hey There. I found your blog using msn. This is an extremely well
written article. I’ll be sure to bookmark it and return to read more of your useful
information. Thanks for the post. I will definitely return.
cheap kamagra: Kamagra gel – Kamagra Oral Jelly
buy kamagra online usa Kamagra Oral Jelly Price Kamagra tablets
Cialis 20mg price: tadalafil iq – Generic Cialis price
https://tadalafiliq.shop/# Tadalafil Tablet
https://yoclick.ru
When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment.
Is there any way you can remove me from that service? Cheers!
Review my webpage; Kundernas
Sildenafil Citrate Tablets 100mg sildenafil iq Viagra Tablet price