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 great blog! Does running a blog similar to this take a
great deal of work? I’ve no understanding of coding however I was hoping to start my own blog soon.
Anyhow, should you have any recommendations or techniques for new blog owners
please share. I know this is off subject but I just needed to ask.
Kudos!
Appreciate the recommendation. Let me try it out.
Feel free to visit my web blog – Bet365 Bingo
I’m all about no deposit grants! They give me a likelihood to win with no risking my possess payment https://files.fm/u/xmrxtxqpj7
ivermectin ebay IverFast ivermectin 0.5
At this moment I am ready to do my breakfast, after having my breakfast coming over again to read more news.
http://cytpremium.com/# buy cytotec pills
prednisone 1 tablet: prednisone – prednisone buy
Как не стать жертвой мошенников при покупке диплома о среднем полном образовании
http://iverfast.com/# minocycline 100 mg without prescription
can you get cymbalta prices
Beyond grateful recognizing efforts given outside realms normal expectations continually pushing boundaries seeking foster change amongst peers encourages growth seen everywhere today reflecting back positively onto futures envisioned collectively Medicare Annual Enrollment
price of stromectol: Ivermectin iver fast – ivermectin otc
generic plavix Plavix Clo cheap plavix antiplatelet drug
Full Service Spa 오피
Burlesque Show 오피커뮤니티
Узнай все о увеличение полового члена хирургическим путем хирургическое увеличение члена
Hello to every body, it’s my first visit of this web site; this
website consists of remarkable and truly fine information in favor
of visitors.
If you’re in the Phoenix location and wish to boost your online existence, you ought to absolutely check out Phoenix SEO services Digitaleer Phoenix Web Design
Покупка диплома о среднем полном образовании: как избежать мошенничества?
Clopidogrel 75 MG price cheapest plavix Cost of Plavix without insurance
https://iverfast.com/# minocycline 100mg for acne
Узнай все о операция по увеличению члена увеличение члена хирургическим путем
Very quickly this web page will be famous amid all blogging and site-building people,
due to it’s fastidious content
my webpage :: luxury assisted living
круглосуточная доставка алкоголя москва недорого
you’re truly a excellent webmaster. The web site loading velocity
is incredible. It seems that you are doing any distinctive
trick. Furthermore, The contents are masterpiece. you have done a
magnificent task in this topic!
Yes, you can download videos on your mobile phone or tablet.
Узнай все о операция по увеличению члена стоимость операции по увеличению члена
I found your discussion around privacy policies attorney ppc management companies
Very nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts.
In any case I’ll be subscribing to your feed and I hope you write again very soon!
Узнай все о операция по удлинению полового члена увеличить член цена