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
Way cool! Some extremely valid points! I appreciate you writing this
post and the rest of the website is extremely good.
Casino Slot Machines goldpay
Hi, just wanted to tell you, I loved this blog post. It was helpful.
Keep on posting!
Private Party 대구유흥
Terrific tips, Cheers.
Hello There. I found your blog using msn. This is a very well written article.
I’ll be sure to bookmark it and come back to read more of your useful information. Thanks
for the post. I will certainly return.
Feel free to visit my website; best realtor in st johns county fl
Nunca había considerado cómo las leyes pueden afectar el valor de la propiedad a largo plazo; gracias por abrirme los ojos a eso! Fuente del artículo
пин ап зеркало: пин ап казино официальный сайт – pinup
Appreciate the thorough analysis. For more, visit Bathroom remodeling
кухни на заказ от производителя – Профессиональное изготовление кухонь на заказ с гарантией качества.
Seasonal allergic rhinitis exacerbation prevention symptom management strategies
” Can’t wait to implement these strategies into my upcoming remodel with assistance from # https://storage.googleapis.com/dumpster-rental-nc/stokesdale/the-ultimate-overview-to-choosing-the-right-dumpster-dimension-for-your.html
It’s hard to come by experienced people for this topic, but you sound like you know
what you’re talking about! Thanks
пин ап казино онлайн: pinup-kazi.kz – pin up казино
Top Money Making Apps in Pakistan, Popular Money Earning Apps in Pakistan
money earning apps in pakistan online earning apps in pakistan .
PG SOFT Gaming – best slot online in the Philippines
better than jili slot and bingo plus
free Spins 3000+ Free Demo Slot
searching pggaming.org on Google or Bing to get free coins for play
https://pggaming.org/
Howdy I am so delighted I found your blog, I really found you by mistake, while
I was researching on Yahoo for something else, Regardless I
am here now and would just like to say thanks for
a tremendous post and a all round interesting blog (I also love the theme/design),
I don’t have time to read it all at the moment but I have bookmarked
it and also added your RSS feeds, so when I have time I will be back to read a great deal more,
Please do keep up the great b.
I’ve been surfing online more than 4 hours today, yet I
never found any interesting article like yours. It’s pretty
worth enough for me. In my view, if all webmasters and bloggers made good content as
you did, the web will be much more useful than ever before.
can you buy coreg online
пин ап казино официальный сайт: пин ап казино – пин ап казино
Asking questions are genuinely nice thing if you are not understanding anything completely,
except this post offers nice understanding even.
Take a look at my web-site … best realtor in volusia county fl
пин ап кз: пинап казино – pinup-kazi.kz
вавада казино зеркало vavada kazi vavada-kazi.ru
пин ап кз: пин ап кз – pinup-kazi.kz
пин ап казино pinup kazi pin up казино
пин ап зеркало: пин ап казино официальный сайт – pinup
вывод из запоя воронеж https://www.aktivnoe.forum24.ru/?1-7-0-00012825-000-0-0-1730749092 .
вавада казино зеркало: вавада онлайн казино – vavada
Как получить диплом стоматолога быстро и официально
Hello to all, how is the whole thing, I think every one is getting more from this web page,
and your views are nice for new users.
My web blog; pronerve 6 ingredients