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
Hi there to every one, the contents present at this web page are really amazing for people knowledge,
well, keep up the good work fellows. benicetomommy.com
bangsar most sort after location
Hello, i think that i saw you visited my web site so i
got here to return the favor?.I’m trying to in finding issues to enhance
my site!I assume its ok to make use of some of your ideas!!
Heya i’m for the primary time here. I found this board
and I to find It truly useful & it helped me out much. I’m hoping
to offer one thing back and aid others like you helped
me.
I am sure this post has touched all the internet visitors, its really
really pleasant article on building up new webpage.
Information well used!.
Всем привет! Подскажите, где найтиразные статьи о недвижимости? Пока нашел – https://karelvoda.ru
Приветствую. Может кто знает, где найтиразные статьи о недвижимости? Сейчас читаю – https://karelvoda.ru
amoxicillin 500mg pill: amoxil com pharm – buy amoxicillin 500mg online
I am truly delighted to look at this blog posts which holds plenty of practical data, thank you for providing these kinds of data.
My website :: http://Support-Groups.org/memberlist.php?mode=viewprofile&u=52004
Приветствую. Подскажите, где найтиполезные блоги о недвижимости? Пока нашел – https://karelvoda.ru
Приветствую. Может кто знает, где найтиполезные блоги о недвижимости? Пока нашел – https://karelvoda.ru
Hi! I just wanted to ask if you ever have any
problems with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup.
Do you have any solutions to protect against hackers?
Very good article. I am dealing with many of these issues as well..
buy prednisone online from canada: cheap prednisone – prednisone 20
What’s up, just wanted to say, I loved this
article. It was funny. Keep on posting!
how to get on dark web drug markets dark web https://darknetmarketstore.com/ – the dark internet
Всем привет! Может кто знает, где найтиразные блоги о недвижимости? Пока нашел – https://karelvoda.ru
clomid: cheap clomid – can you buy cheap clomid without dr prescription
Приветствую. Может кто знает, где найтиразные статьи о недвижимости? Пока нашел – https://karelvoda.ru
кухня заказать – это просто и удобно с профессионалами, которые учтут все детали.
I’m truly enjoying the design and layout
of your blog. It’s a very easy on the eyes which makes it
much more pleasant for me to come here and visit more often. Did you hire
out a designer to create your theme? Excellent work!
Visit my homepage … random video chat
Эскорт-услуги это сервис, который предоставляет компанию для времяпровождения в различных сферах, будь то деловая
встреча, свадьба или просто вечерний выход в свет.
В Москве этот рынок активно развивается, и многие выбирают его по
ряду причин. Рассмотрим главные плюсы использования эскорт-услуг.
priligy: cheap priligy – priligy max pharm
Hi there I am so thrilled I found your weblog, I really
found you by mistake, while I was browsing on Bing for something else, Regardless I am here now and would just
like to say many thanks for a marvelous post and a all round enjoyable
blog (I also love the theme/design), I don’t have time to read it all
at the moment but I have saved it and also added your RSS feeds, so when I have time I will
be back to read a lot more, Please do keep up the awesome job.
Процесс получения диплома стоматолога: реально ли это сделать быстро?
burgasdent.listbb.ru/viewtopic.php?f=3&t=700
Легальные способы покупки диплома о среднем полном образовании
net4women.ru/blogs/5778/Успех-с-дипломом-в-руках
Appreciate the helpful advice. For more, visit mobile tire fix
I do trust all tһe ideas yoս’ve рresented in уoսr
post. Theyy are really convincing and will deffinitely ᴡork.
Nߋnetheless, thhe posts are too bгief fօr newbies.
May you plеase prolong tһem a bіt from subsequent tіme?
Thankѕ for thee post.
Feel free to surf tο my homepaɡe: đồng hồ nữ hàng hiệu
Узнайте, как безопасно купить диплом о высшем образовании