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
Heya are using WordPress for your blog platform?
I’m new to the blog world but I’m trying to get started and
set up my own. Do you require any coding knowledge to make your own blog?
Any help would be really appreciated!
Robótki ręczne to nie tylko hobby robótki ręczne na szydełku
I think that is one of the most important info for me.
And i’m satisfied studying your article. But want to statement on some
normal things, The site style is ideal, the articles is really excellent :
D. Excellent activity, cheers
many way to consider when buying penthouse
Grеetings! I knoԝ this is sߋmewhat off tipic but I was ѡondewring iif you knew where I cоuld get a captcha
plugin for my commkent form? I’m using the same blg platform
aѕ yοurs and I’m havbing trouble fіndkng one?
Thanks a lot!
Also vist my homepage: iconwin link alternatif
Every weekend i used to pay a quick visit this web site, because i want enjoyment, for the reason that this
this website conations really nice funny data
too.
Предлагаем услуги профессиональных инженеров офицальной мастерской.
Еслли вы искали официальный сервисный центр lg, можете посмотреть на сайте: сервисный центр lg в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Experience hassle-free software installation and unparalleled support with Software Sale Mart.
Купить диплом старого образца, можно ли это сделать по быстрой схеме?
Покупка школьного аттестата с упрощенной программой: что важно знать
http://lisinopril1st.com/# zestril 10mg price
I absolutely love your website.. Pleasant colors & theme.
Did you develop this site yourself? Please reply back as I’m looking to create my own personal website and would
love to learn where you got this from or just what the theme is called.
Many thanks!
https://w3.syairsetan.live/
https://lisinopril1st.com/# cheapest Lisinopril
Быстрое обучение и получение диплома магистра – возможно ли это?
Hello there! Do you use Twitter? I’d like to follow you if that
would be okay. I’m absolutely enjoying your blog and look forward to new posts.
I used to be suggested this web site by my cousin. I’m now not certain whether or not this submit
is written via him as no one else realize such special about my trouble.
You’re wonderful! Thank you!
Стоимость дипломов высшего и среднего образования и как избежать подделок
and if personally already have a buy doxycycline online uk on operating system android, study address amazon appstore
for android, because that the right tablets require the right apps!
cheapest Lisinopril cheapest Lisinopril Lisinopril 1st
http://lisinopril1st.com/# Lisinopril 1st
buy cheap amoxicillin online: buy amoxil online – medicine amoxicillin 500
I’m planning to do it yourself my roofing system repair work, however I’m nervous about it! Any recommendations from knowledgeable roofing professionals would be appreciated roof repairs in tampa
Официальная покупка школьного аттестата с упрощенным обучением в Москве
many have own test of design and furnishing
steroids prednisone for sale: buy prednisone – prednisone capsules
http://lisinopril1st.com/# buy Lisinopril 1st
What you wrote made a ton of sense. But, consider this, what if you
added a little content? I am not saying your information is
not good., however what if you added something
that grabbed people’s attention? I mean JS 依照元素出現數量排序 – 馬老師
雲端研究室 is kinda boring. You ought to peek at Yahoo’s home page and note how they create post headlines to get people interested.
You might try adding a video or a pic or two to grab
people interested about what you’ve got to say. In my opinion, it could make your blog a little bit more interesting.
http://lisinopril1st.com/# Lisinopril 1st
Pretty section of content. I just stumbled upon your web site
and in accession capital to assert that I acquire actually enjoyed account your blog posts.
Anyway I’ll be subscribing to your feeds and even I achievement you access consistently quickly.
What’s up colleagues, pleasant paragraph and good
urging commented at this place, I am genuinely enjoying by these.