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
On our website you will find unique collection rare coins from all over the world. We offer both modern and historical specimens that will become real finds for collectors.
For more information, follow the link [url=https://numizmatexpert.ru/]юбилейные рубли ссср – каталог [/url]
Our team of experts carefully selects each coin, ensuring authenticity.
Jugabet app Jugabet app .
Официальная покупка аттестата о среднем образовании в Москве и других городах
Jega bet Jega bet .
найкращі фільми онлайн дивитися кіно онлайн
amoxicillin 500mg tablets price in india: AmoxStar – amoxicillin online without prescription
Легальная покупка диплома о среднем образовании в Москве и регионах
can you buy amoxicillin uk: amoxicillin medicine – where to buy amoxicillin pharmacy
Never underestimate power knowledge brings forth when tackling potential hazards presented through Mother Nature herself attempting disrupt lives/homes frequently!! ##AnyKeyWord### Water restoration
order amoxicillin online Amox Star amoxicillin script
https://amoxstar.com/# azithromycin amoxicillin
buy cipro without rx CiPharmDelivery buy ciprofloxacin
Легальные способы покупки диплома о среднем полном образовании
Продвижением сайта с оплатой за результат лучшее
Ten Things You Need To Be Aware Of Back Injury Law
Firm Injury Attorney Near Me
Jugabet tragamonedas Jugabet tragamonedas .
I delight in, cause I found exactly what I was
looking for. You’ve ended my 4 day long hunt! God Bless you man. Have
a nice day. Bye
I found some great deals on moving services at westchester local moving companies – worth checking
Tragamonedas Jugabet Tragamonedas Jugabet .
https://cipharmdelivery.com/# buy cipro no rx
онлайн фільм безкоштовно якості фільми 2024 дивитися онлайн
I loved as much as you will receive carried out right here. The sketch is attractive, your authored material stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly a lot often inside case you shield this increase.
can i buy clomid for sale clomid on pharm order cheap clomid without prescription
buy cipro online usa: ci pharm delivery – buy cipro
Возможно ли купить диплом стоматолога, и как это происходит
Jugabet Jugabet .
Сколько стоит диплом высшего и среднего образования и как его получить?
Jugabet bonos Jugabet bonos .
Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
You obviously know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving
us something enlightening to read?
Feel free to surf to my homepage: 隱形眼鏡價格
http://amoxstar.com/# where can i buy amoxicillin over the counter uk