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
Ꮃhat’s up to аll, for the reason that Ӏ am in fаct eager ᧐f
reading this blog’ѕ post tο be updated on ɑ
regular basis. It consists оf fastidious data.
Aⅼso visit my web site … Jackpot bet
Наши специалисты предлагают выгодно заказать диплом, который выполняется на бланке ГОЗНАКа и заверен печатями, водяными знаками, подписями. Данный документ пройдет лубую проверку, даже с применением специфических приборов. androidinweb.ru/ofitsialnyie-diplomyi-kupit-po-vashemu-zaprosu
1win онлайн 1win онлайн .
dark web market urls darknet drug links
olympe casino: olympe – olympe casino
Saved as a favorite, I like your web site!
1win uganda 1win uganda .
этапы согласования перепланировки квартиры этапы согласования перепланировки квартиры .
melbet kg http://melbet1003.ru .
darknet site darknet drug links
olympe casino casino olympe
olympe casino: casino olympe – casino olympe
https://olympecasino.pro/# olympe casino
Cabinetry contractors focus on details and space-planning skills can help you squeeze
functionality from even the tiniest corners of your
Redwood City, CA home.
мостбет войти https://mostbet5009.ru .
olympe casino en ligne: casino olympe – olympe casino
I was recommended this website by my cousin. I’m not sure whether this
post is written by him as nobody else know such detailed about my difficulty.
You’re incredible! Thanks!
I relish, lead to I discovered just what I was looking for.
You have ended my 4 day lengthy hunt! God Bless you man. Have a
great day. Bye
olympe casino avis: casino olympe – olympe casino en ligne
olympe casino: olympe casino cresus – olympe casino
I have to thank you for the efforts you have put in penning this website.
Intriguing material, I’ll be back to inspect a
lot more.
mostbet скачать http://mostbet7004.ru/ .
https://olympecasino.pro/# olympe casino cresus
olympe casino cresus: olympe casino en ligne – olympe casino cresus
darknet markets url bitcoin dark web
мелбет кж http://www.melbet1003.ru .
magnificent post, very informative. I wonder why the other experts of
this sector do not understand this. You must continue your writing.
I am confident, you’ve a huge readers’ base already!
Hi everyone, it’s my first visit at this site,
and article is genuinely fruitful in favor of me, keep up posting these posts.
Good tale, however it is not extremely clear regarding the desi bf sex experience.