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
I’m excited to find this website. I want to to thank you
for ones time for this particularly fantastic read!!
I definitely really liked every part of it and I have you
saved to fav to see new stuff on your blog.
The Reasons Why Suzuki Celerio Replacement Key Will Be Everyone’s Desire
In 2023 Suzuki Car Keys Replacement
trezor.io/start
rx discount pharmacy: toronto pharmacy viagra – kroger pharmacy gabapentin
An Intermediate Guide To Sectional L Shaped Sofa l shaped sectional leather – Caitlyn,
doc morris pharmacy artane: real pharmacy rx generic viagra – ED Trial Pack
Good post. I absolutely appreciate this website. Thanks!
Hello! Someone in my Myspace group shared this website
with us so I came to take a look. I’m definitely enjoying
the information. I’m bookmarking and will be tweeting this
to my followers! Exceptional blog and excellent design and style.
online medicine tablets shopping: Viagra Soft Flavored – cialis india pharmacy
10 No-Fuss Methods For Figuring Out Your Faux Leather Couch real leather couch (http://www.eden1004.kr/)
I want to to thank you for this good read!! I certainly enjoyed every bit of it.
I have you book-marked to check out new things you post…
The 10 Most Scariest Things About Adult.ADHD Test Female Adhd Test Uk
It’s genuinely very complicated in this busy life to listen news on TV, therefore I only use internet for that purpose, and get the most recent news.
rx software pharmacy: reliable online pharmacy accutane – community rx pharmacy
pharmacy cost of cialis: pioneer rx pharmacy software – legal online pharmacy review
Amazing things here. I am vеry happy to see your post.
Thanks sо uch and I аm looking abead to contact you.
Will you kindly drop me a e-mail?
Wow, that’s what I was seeking for, what a stuff!
existing here at this website, thanks admin of this web site.
I love your blog.. very nice colors & theme. Did you create this website yourself or
did you hire someone to do it for you? Plz reply as I’m
looking to create my own blog and would
like to know where u got this from. thank you
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You clearly know what youre talking about, why waste your intelligence on just posting
videos to your blog when you could be giving us something enlightening to read?
Уборка помещений пожара http://uborka-ot-pozhara.ru/
cialis online pharmacy usa: online pharmacy store in delhi – can i buy viagra in pharmacy
женский ретрит https://ретриты.рф
Whoa! This blog looks exactly like my old one! It’s on a completely different subject but it has pretty much
the same page layout and design. Excellent choice of
colors!
Guide To Cost Of Private ADHD Assessment UK: The Intermediate Guide On Cost Of Private ADHD Assessment
UK Private Adhd Assessment Uk
WOW just what I was looking for. Came here by searching for Blog
Excellent website you have here but I was curious if you knew of any forums that
cover the same topics talked about here? I’d really like to be a part of
online community where I can get feedback from other experienced individuals that share
the same interest. If you have any suggestions, please let me know.
Thank you!
restore rx pharmacy: Tofranil – rite aid pharmacy viagra cost
Wonderful blog! I found it while browsing on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there!
Many thanks
Официальное получение диплома техникума с упрощенным обучением в Москве
xalatan online pharmacy: good rx pharmacy – australia online pharmacy viagra