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
Hello, I read your blogs on a regular basis. Your writing style is awesome, keep up the good work!
Hi there would you mind stating which blog platform you’re using?
I’m going to start my own blog soon but I’m having
a tough time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most
blogs and I’m looking for something completely unique. P.S Apologies for getting off-topic but I had
to ask!
hey there and thank you for your info – I’ve definitely picked up anything new from right here.
I did however expertise a few technical issues using this site, since I experienced to reload the website a lot of times previous to I could get it to load properly.
I had been wondering if your web host is OK? Not that I am
complaining, but slow loading instances times will often affect your placement in google and can damage your
quality score if ads and marketing with Adwords. Anyway I’m adding this RSS to my
email and can look out for much more of your respective fascinating content.
Ensure that you update this again soon.
cialis discount pharmacy: wedgewood pharmacy gabapentin – online pharmacy pain relief
20 Fun Details About Audi Key audi car key Holder (g28carkeys69632.ssnblog.com)
manor pharmacy store locator: the pharmacy – cephalexin pharmacy
buy viagra online
The 10 Most Terrifying Things About Titration Meaning ADHD titration meaning adhd (https://www.instapaper.com/)
15 Terms Everyone Who Works In Desk Treadmill Industry Should Know under desk Treadmill
Why Nobody Cares About Bluetooth Fucking Machine Sex Machine Adult
abortion pill online pharmacy: Prasugrel – pharmaceuticals online australia
I am really glad to glance at this web site posts which carries plenty of valuable information, thanks for
providing these kinds of information.
Hi there, all is going nicely here and ofcourse every one is sharing information, that’s truly fine, keep up writing.
Привет!
Мы предлагаем документы техникумов
varanasitutor.com/questions/kupit-diplom-582279iol
obviously like your web-site but you need to take a look at
the spelling on several of your posts. Many of them are
rife with spelling issues and I to find it very bothersome
to tell the truth nevertheless I’ll certainly come back again.
In Which Location To Research Treat Anxiety Online is anxiety Treatable – http://www.Annunciogratis.net,
tamoxifen online pharmacy: viagra from pharmacy – legitimate online pharmacies
This article is actually a good one it assists new internet users, who are wishing in favor of blogging.
Terrific post but I was wondering if you could write a litte more on this subject?
I’d be very thankful if you could elaborate a little bit further.
Thank you!
pharmacy rx 1: medicine online order – abilify india pharmacy
The Guide To Car Key Cutting Machine In 2023 cheapest key cutting Near me
australian pharmacy online: sam’s club pharmacy propecia – fred’s dollar store pharmacy
Thanks for a marvelous posting! I actually enjoyed reading it, you will be a
great author. I will be sure to bookmark your blog and will
eventually come back very soon. I want to encourage that you continue your great job, have a nice holiday weekend!
Neurontin: online overseas pharmacy – pharmacies
valacyclovir indian pharmacy: xeloda specialty pharmacy – best indian pharmacy online
Hola! I’ve been reading your web site for a while now and finally got the
bravery to go ahead and give you a shout out from Austin Tx!
Just wanted to say keep up the excellent job!
Hello, its nice paragraph regarding media print,
we all be familiar with media is a wonderful source of facts.
Here is my web-site 成人卡通 (porntube.red)
I think everything said was actually very logical.
However, what about this? suppose you composed a catchier
title? I mean, I don’t wish to tell you how to run your website, but what if you added
a post title to possibly get a person’s attention? I mean JS 依照元素出現數量排序 – 馬老師 雲端研究室
is a little plain. You ought to peek at Yahoo’s home page and watch how they write post headlines to get viewers to open the links.
You might try adding a video or a related picture or two to get people excited about what you’ve got to say.
In my opinion, it might bring your posts a little bit more interesting.
finasteride online pharmacy india: online pharmacies uk – pharmacy store design
reputable indian online pharmacy: pharmacy dispensing clozapine – mirtazapine online pharmacy