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
Узнай все о выпрямление носовой перегородкиискривление перегородки носа
If you wish for to increase your familiarity only keep
visiting this web page and be updated with the most recent information posted
here.
Cet article est une mine d’or pour tout artisan voulant se digitaliser.
Des conseils pratiques et pertinents!
Процесс получения диплома стоматолога: реально ли это сделать быстро?
If you wish for to take a great deal from this paragraph then you have to
apply such methods to your won web site.
Hey there! This is my first visit to your blog!
We are a collection of volunteers and starting a new project in a community
in the same niche. Your blog provided us valuable information to work
on. You have done a marvellous job!
buy plavix: п»їplavix generic – buy Clopidogrel over the counter
plavix medication: buy plavix online – Clopidogrel 75 MG price
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Social Gathering 부산달리기
Купить диплом старого образца, можно ли это сделать по быстрой схеме?
I was extremely pleased to discover this great site. I want to to thank you for ones time for this fantastic read!!
I definitely enjoyed every part of it and I
have you book-marked to see new information on your blog.
Also visit my web page بهترین بروکر فارکس
After I initially left a comment I seem to have clicked on the -Notify me when new comments are added-
checkbox and now each time a comment is added I recieve four emails
with the exact same comment. Is there a way you are able to remove me from that service?
Many thanks!
Как приобрести диплом о среднем образовании в Москве и других городах
Сколько стоит диплом высшего и среднего образования и как его получить?
I have been browsing online more than three hours today, yet
I never found any interesting article like yours. It is pretty worth enough for me.
Personally, if all webmasters and bloggers made good content as you did,
the net will be much more useful than ever before.
Plavix generic price buy Plavix Clo generic plavix
prednisone 5084: prednisone – prednisone for sale no prescription
antiplatelet drug: Clopidogrel Best Prices – Cost of Plavix on Medicare
Процесс получения диплома стоматолога: реально ли это сделать быстро?
how to buy generic clomid no prescription: clomid – can i get clomid
When you are looking for a poster of Kanye West, you will notice tons of stylish pieces.
Whether it’s inspired by his legendary records like The Life of Pablo, to modern collections, there’s something that matches any music lover.
Kanye West print definitely can enhance your interior.
Купить диплом магистра оказалось возможно, быстрое обучение и диплом на руки
tor darknet darkmarket https://darknetmarketstore.com/
закачать приложения онлайн казино https://atiragusa.it/kazino-bez-vlozhenij-2024-igrat-onlajn-v-igrovyh-7/
Hey I know this is off topic but I was wondering if you knew of any widgets I
could add to my blog that automatically tweet my newest twitter updates.
I’ve been looking for a plug-in like this for quite some time
and was hoping maybe you would have some experience with something like this.
Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.
Легальная покупка диплома ПТУ с сокращенной программой обучения
Купить диплом старого образца, можно ли это сделать по быстрой схеме?
discount zestril: cheapest Lisinopril – can you order lisinopril online
Узнай все о операция на носовую перегородкуисправление носовой перегородки цена