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
%%
Here is my website: best dolls For customizing
What’s up mates, its wonderful paragraph regarding educationand fully explained, keep it up all the time.
You really make it seem so easy with your presentation but I find this topic to be actually something that I think I would never understand.
It seems too complex and extremely broad for
me. I’m looking forward for your next post, I’ll try to get the hang of it!
%%
Review my website :: seo price uk
Whаt’s սp to еvery one, fⲟr tһe reason thnat I am ɑctually keen of reading tһis webpage’ѕ
post to be updated regularly. It incⅼudes fastidious stuff.
Heree іs my site :: cryptometrics101
%%
Also visit my website: Realldoll
you are in point of fact a excellent webmaster.
The site loading pace is amazing. It sort of feels that you are doing any distinctive trick.
Moreover, The contents are masterpiece. you have done a magnificent job on this subject!
Truly when someone doesn’t know after that its up
to other users that they will help, so here it happens.
Someone essentially lend a hand to make critically articles I’d state.
This is the very first time I frequented your web page and so far?
I surprised with the analysis you made to create this particular publish amazing.
Great task!
%%
Also visit my site buy ing sex doll s
%%
Also visit my blog post … sexdoll custom (https://asdf.philgo.com/?module=adv&action=count_Click_submit&idx=1271467101&url=https://www.keralaplot.com/user/profile/2363616)
Hey would you mind letting me know which webhost you’re working with?
I’ve loaded your blog in 3 different internet browsers and
I must say this blog loads a lot faster then most.
Can you suggest a good internet hosting provider at a honest price?
Many thanks, I appreciate it!
%%
Also visit my site seo prices uk
%%
my website; Real Live Sex Dolls
%%
Also visit my page – best sex dolls Review
%%
Feel free to visit my page; buy Love doll
Hi there, yup this piece of writing is genuinely nice and I have learned lot of things
from it on the topic of blogging. thanks.
Ӏ don’t even қnow the way I ended up here, howevеr Ι believed this post was good.
I don’t understand ԝwho you’re but definitеoy you are going to a well-known blogger in the еvent you are not already.
Cheers!
%%
Also visit my web page :: Buy Sex dolls
%%
my web site … love doll review
When someone writes an paragraph he/she keeps the idea of a user in his/her
brain that how a user can know it. Therefore that’s why this piece of writing is perfect.
Thanks!
%%
my homepage :: spf30 moisturiser
I’ve been exploring for a little bit for any high quality articles or weblog posts on this kind of area .
Exploring in Yahoo I at last stumbled upon this website. Reading this information So i am satisfied to show that I’ve an incredibly
just right uncanny feeling I came upon exactly what I needed.
I most no doubt will make certain to do not put out of your mind this site and provides
it a look regularly.
%%
Feel free to visit my web page; customizing Sexdoll
%%
Here is my homepage :: best night cream uk
%%
my website … Se x dolls
%%
Also visit my web-site: Togel
%%
Here is my website: best night cream
база для хрумера купить
Вы занимаетесь продвижением сайтов?
Тогда вам к нам. [url=https://goo.su/4zRx]Наша группа в телеграмме[/url].У нас вы можете выбрать
любую нужную для вашей работы базу для прогонов Xrumer и GSA, allsubmitter по
разной ценовой категории.успешные базы для хрумера
https://buddypress.org/members/stocktradestar/profile/