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
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Как купить диплом о высшем образовании с минимальными рисками
вавада казино зеркало: вавада казино зеркало – vavada kazi
Appreciate the useful tips. For more, visit House remodel
пин ап казино онлайн: pinup-kazi.kz – pinup kazi
pinup-kazi.ru: пинап казино – пин ап зеркало
вавада казино онлайн: вавада казино – вавада казино
Hi, i feel that i noticed you visited my blog thus i got here to return the want?.I am trying to to
find issues to enhance my site!I suppose its adequate to make
use of some of your ideas!!
Feel free to surf to my web blog best realtor in Meridian Ms
pinup: пин ап казино официальный сайт – pinup
казино вавада: vavada-kazi.ru – вавада казино онлайн
pinup kazi: pin up казино – pin up казино
Пошаговая инструкция по безопасной покупке диплома о высшем образовании
Pretty nice post. I just stumbled upon your blog and wanted to say that
I’ve really enjoyed surfing around your blog posts. In any case I’ll be subscribing to your feed and I hope you write
again soon!
https://ohana99.net/เว็บสล็อตมากความสามารถที่ให้มากกว่าความสนุกสนาน
Купить дженерики с доставкой по Санкт-Петербургу качественные препараты для повышения потенции
на сайте https://men78.ru высокое качество производства Индии в наличии в СПБ
I’m planning to install solar panels roofing company near me
Процесс получения диплома стоматолога: реально ли это сделать быстро?
https://www.mptextile.ru — Посетите сайт для подробной информации о кухнях.
пинап казино: пин ап казино онлайн – pinup kazi
I truly love your website.. Pleasant colors & theme.
Did you create this site yourself? Please reply back as I’m wanting to create my own personal blog and
would love to learn where you got this from or exactly what the theme is called.
Kudos!
Can I simply say what a comfort to uncover someone who actually knows what
they’re discussing on the internet. You definitely realize how to bring
a problem to light and make it important. More people should
read this and understand this side of your story. It’s surprising you’re not more popular because
you certainly possess the gift.
пин ап кз: pinup-kazi.kz – пин ап кз
Really informative post! SEO is not just optional; it’s essential for modern law firms looking to thrive online. More info at affordable attorney seo firms
I’ve been exploring for a little bit for any high quality articles or
blog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this website.
Studying this info So i’m glad to express that I have a very good uncanny feeling
I came upon exactly what I needed. I most for sure will make certain to do not overlook this website and give it a
look regularly.
Here is my web blog what is better phen375 or phenq
пин ап казино: пин ап кз – пинап казино
пинап казино: pinup kazi – пин ап зеркало
Kalau ada situs yang tiba-tiba minta data pribadi atau finansial, lebih baik kamu hati-hati.
Scam biasanya suka kayak gitu, bawa kita ke halaman palsu
buat curi data.
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Drugs information leaflet. Long-Term Effects.
azithromycin and b vitamins
Everything news about pills. Get information here.
Really when someone doesn’t know after that its up to other people that
they will assist, so here it occurs.