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
Lisinopril 1st: buy Lisinopril 1st – buy Lisinopril online
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Легальные способы покупки диплома о среднем полном образовании
Ꮃow, fɑntastic weblog ⅼayout! How long hɑve you eveг been bloɡging for?
you make blⲟggіnbg look easy. The entire glance of your websitе is wonderful, let alone the contеnt material!
Here is my Ƅlog; dede4d
cheapest Lisinopril: cheapest Lisinopril – prinivil drug cost
Hi i am kavin, its my first time to commenting anywhere, when i read this article i thought i could also
make comment due to this sensible article.
Как правильно купить диплом колледжа и пту в России, подводные камни
Официальная покупка диплома вуза с упрощенной программой обучения
bitcoin dark web deep web drug markets tor markets links
I’ve been browsing online more than 4 hours today, yet
I never found any interesting article like yours. It
is pretty worth enough for me. Personally, if all website owners and bloggers made good
content as you did, the net will be a lot more useful than ever before.
I am really glad to read this webpage posts which contains plenty of
helpful facts, thanks for providing such information.
buy priligy: priligy max pharm – buy priligy max pharm
Hi, its pleasant article on the topic of media print, we all know media is a
great source of data.
The mind Head injury attorney Florida attorneys at
DE CARO & KAPLEN, LLP.
buy cytotec over the counter: cyt premium – buy cytotec online fast delivery
http://cytpremium.com/# buy cytotec in usa
where buy singulair no prescription
bokep terbaik sma toket gede menyala banget
Как получить диплом о среднем образовании в Москве и других городах
Piece oof wrіting ԝriting is alѕo a fun,
if you be famіⅼiar with after that yoս can ѡrite otherwise it is
difficult to write.
mʏ website … dede4d (classicalmusicmp3freedownload.com)
Mr Ring was born Jan 18, 1917, at Wilbur and remained there till his graduation from Wilbur High school .
http://lisinopril1st.com/# buy Lisinopril online
where to get generic zocor online
cheap priligy: dapoxetine price – Priligy tablets
https://lisinopril1st.com/# zestril tab 10mg
my web page … http://Sunad1.com/white/board/goto.php?url=aHR0CHM6Ly94aWFtcC5uZXQvZ28vYUhSMGNITTZMeTkzZDNjdWNHOXJaWEowZFdKbExtTnZiUzg&encoded=1
It’s an amazing article in support of all the web viewers; they will take benefit from it I
am sure.
Your way of telling the whole thing in this paragraph is truly nice, every one be capable of effortlessly understand it, Thanks
a lot https://www.provenexpert.com/nl-nl/pub018/
You made some really good points there. I checked on the net for more information about the issue and found most people will
go along with your views on this website.
I do trust all of the concepts you’ve presented for your post.
They are really convincing and will certainly work.
Still, the posts are too quick for novices. May just you please prolong them
a bit from next time? Thanks for the post.