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
Hey There. I found your blog using msn. This is a very well written article.
I’ll make sure to bookmark it and return to read more of your useful information. Thanks
for the post. I will definitely return.
Kylian Mbappe Lotten https://kylianmbappe.prostoprosport-br.com Futebolista frances, atacante do Paris Saint-Germain e capitao da selecao francesa equipe . Em 1? de julho de 2024, ele se tornara jogador do clube espanhol Real Madrid.
Medicine information for patients. Long-Term Effects.
how to buy generic ventolin without insurance
Some trends of drug. Get information here.
buying generic finasteride without prescription
Carlos Henrique Casimiro https://carloscasemiro.prostoprosport-br.com Futebolista brasileiro, volante do clube ingles Manchester United e capitao do Selecao Brasileira. Pentacampeao da Liga dos Campeoes da UEFA, campeao mundial e sul-americano pela selecao juvenil brasileira.
can i order femara without dr prescription
sildalis without prescription
I like the helpful information you provide in your articles.
I’ll bookmark your weblog and check again here frequently.
I’m quite sure I will learn a lot of new stuff right here!
Good luck for the next!
Guide To Slot Mechanics: The Intermediate Guide For Slot
Mechanics Slot Mechanics
What’s The Current Job Market For Velvet Sectional Sofa Professionals Like?
velvet Sectional sofa
Максимальный возможный выигрыш с кэш-бонуса составляет х10 от его суммы.
Medicines information. Effects of Drug Abuse.
zithromax tablets price
Some news about medicines. Get here.
There Are A Few Reasons That People Can Succeed With The 4mph Mobility Scooter Industry 4Mph Scooter
actos 30 mg side effects
amoxil capsule 500 mg
Having read this I thought it was really enlightening.
I appreciate you spending some time and energy to put this short article
together. I once again find myself personally spending a significant amount of time both reading and posting comments.
But so what, it was still worth it!
8 Tips To Improve Your Bunk With Double Bed Game white double Bunk Bed