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
how to buy cheap clomid tablets: generic clomid – cost clomid without insurance
Appreciating the time and energy you put into your site and
detailed information you offer. It’s great to come across a blog every once in a
while that isn’t the same out of date rehashed material.
Great read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.
how to buy amoxicillin online: com pharm – cost of amoxicillin 30 capsules
Как официально приобрести аттестат 11 класса с минимальными затратами времени
First of all I would like to say great blog! I had a quick question in which I’d like to ask if you
don’t mind. I was interested to know how you center yourself and clear your mind prior to writing.
I’ve had trouble clearing my mind in getting my thoughts out there.
I truly do enjoy writing however it just seems
like the first 10 to 15 minutes are generally lost simply just trying to figure out how to begin. Any ideas or tips?
Thanks!
This is very attention-grabbing, You are a very
professional blogger. I have joined your rss feed and
stay up for seeking extra of your fantastic post. Also, I
have shared your site in my social networks
Very descriptive article, I liked that a lot.
Will there be a part 2?
Have you ever considered about adding a little bit more
than just your articles? I mean, what you say is
fundamental and everything. Nevertheless imagine
if you added some great images or videos to give your posts
more, “pop”! Your content is excellent but with pics and video clips, this website could certainly be one of the
best in its field. Amazing blog!
can i order generic clomid online: buy clomid – cost generic clomid without dr prescription
Производство павильонов с доставкой и установкой
Nice blog here! Also your website loads up fast!
What web host are you using? Can I get your affiliate link to your host?
I wish my site loaded up as fast as yours lol
Drug trends described. Patient medication guide.
buy proscar online
Short-term impacts described. Latest medication news.
dapoxetine price: priligy max pharm – buy priligy
cheap priligy: Priligy tablets – buy priligy
Informative article, exactly what I wanted
to find.
Telegram 是一款主打隐私保护和高安全性的即时通讯应用,广泛支持全球用户。虽然 Telegram 没有发布专门的“中文版”,但其支持简体中文和繁体中文语言,用户可以通过设置轻松切换到中文界面。Telegram 提供了包括加密聊天、语音通话、文件传输、大型群组和频道等丰富的功能,并且支持跨平台同步,用户可以在手机、电脑等设备上无缝使用。https://www.telegramoo.com
get generic clomid tablets: generic clomid – where buy clomid price
max pharm: cheap priligy – priligy max pharm
Hello there, You’ve done an excellent job. I’ll definitely digg it and personally suggest to my friends.
I’m sure they will be benefited from this website.
prednisone cost 10mg: cheap prednisone – prednisone cost us
I like reading an article that will make men and women think.
Also, many thanks for permitting me to comment!
Hello, i read your blog from time to time and i own a similar one and
i was just curious if you get a lot of spam comments? If so how do you protect against it, any plugin or anything you can suggest?
I get so much lately it’s driving me mad so any help is very much
appreciated.
Приветствую. Может кто знает, где найтиразные блоги о недвижимости? Пока нашел – https://geotech48.ru
dapoxetine online: max pharm – priligy
Приветствую. Может кто знает, где почитатьполезные блоги о недвижимости? Пока нашел – https://geotech48.ru
can i order cheap tadacip for sale
You should take part in a contest for one of the greatest
blogs on the web. I am going to highly recommend
this website!
Пошаговая инструкция по безопасной покупке диплома о высшем образовании
Полезные советы по безопасной покупке диплома о высшем образовании
Официальная покупка диплома вуза с сокращенной программой обучения в Москве