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
click here for info phantom wallet
right here MetaMask Download
Very quickly this web site will be famous among all blogging and site-building people, due to it’s pleasant articles or reviews
az parayla cok kazandiran slot oyunlar?: az parayla cok kazandiran slot oyunlar? – az parayla cok kazandiran slot oyunlar?
What’s up everyone, it’s my first pay a visit at this website, and post is in fact fruitful
in favor of me, keep up posting such content.
вывод из запоя капельница вывод из запоя капельница .
I do agree with all of the concepts you’ve offered to your post.
They’re really convincing and can definitely work. Nonetheless, the posts are too short for starters.
Could you please lengthen them a bit from next time?
Thanks for the post.
useful reference MetaMask Download
5 For You To Host A Yard Bbq On The Budget HiOP
Very energetic article, I enjoyed that bit. Will
there be a part 2?
вывод из запоя на дому цена вывод из запоя на дому цена .
It’s a pity you don’t have a donate button! I’d definitely donate to
this superb blog! I suppose for now i’ll settle for book-marking and adding your RSS feed
to my Google account. I look forward to new updates and will
share this website with my Facebook group. Talk soon!
вывести из запоя [url=https://vyvod-iz-zapoya-rostov229.ru]вывести из запоя[/url] .
Thanks for the clear advice. More at Gordon Law
100 먹튀검증 먹튀사이트 목록 확인 커뮤니티 먹튀모음 토토사이트 메이저사이트 스포츠 미니게임 파워볼 꽁머니사이트
모든 사이트 먹튀검증을 시행하고 있으며, 가장 빠른 먹튀검증사이트입니다 먹튀모음은 먹튀에서 안전한 공식 보증업체를 추천해드리며, 부당한 일을
당하지 않게 체계적인 시스템으로 운영하고 있습니다
Has anyone considered sustainable options when hiring a Remodeling companies
пропуск в москву для грузовиков пропуск в москву для грузовиков .
useful content MetaMask Download
Thank you for discussing the cultural importance of marijuana in distinctive societies! It’s a prosperous topic to explore extra; see greater at weed delivery
en guvenilir casino siteleri: Casino Siteleri – canl? casino siteleri
Portugal has long been a country known for producing exceptional talent in the world of sports. With a rich history in football, many athletes from this nation have achieved international fame through their incredible skills and accomplishments http://www.asystechnik.com/index.php?title=_How_Lusitano_Horses_Excel_in_Bullfighting
Bar Hopping hiop
Hello, i feel that i noticed you visited my web site thus i got here to go back the
favor?.I am attempting to to find things to enhance my website!I guess its adequate to use
a few of your ideas!!
I really like what you guys tend to be up too. This type of clever
work and exposure! Keep up the superb works guys I’ve you guys to my
own blogroll.
вывод из запоя ростов на дону вывод из запоя ростов на дону .
нарколог на дому капельница цена нарколог на дому капельница цена .
Great job explaining the role of additives in concrete mixtures! Such an informative read! More details can be found at my site: concrete contractor
снятие интоксикации на дому снятие интоксикации на дому .
Everything It Is Advisable To Know About
Melbourne Nightlife HiOP
Asbestos Attorney Explained In Fewer Than 140 Characters asbestos lawsuits