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
I do not know if it’s just me or if everybody else encountering problems with your blog.
It appears like some of the text within your posts are running off the screen. Can somebody else please comment and let me know
if this is happening to them as well? This could be a problem with my browser because I’ve had
this happen previously. Thanks
Ten Adult Adhd Assessment Myths You Shouldn’t Share On Twitter assessment adhd
сколько стоит узаконить перепланировку квартиры http://www.stoimost-soglasovaniya-pereplanirovki-kvartiry.ru .
matadorbet bid matadorbet.bid matadorbet giris
The 10 Most Dismal Treadmill Incline Failures Of All Time Could Have Been Prevented what Does treadmill incline mean
Casino Siteleri: Canl? Casino Siteleri – Deneme Bonusu Veren Siteler
Реально ли приобрести диплом стоматолога? Основные этапы
x70795vj.beget.tech/2024/10/20/oficialnyy-diplom-legko.html
When some one searches for his essential thing,
thus he/she wants to be available that in detail, so that thing is maintained over here.
Hello There. I found your blog using msn. This is a very well
written article. I will be sure to bookmark it and
come back to read more of your useful info. Thanks for the post.
I’ll certainly comeback.
Погрузитесь на юдоль скорби веселий также увлекательных 3 сверху портале Любовь 28!
инвестиции
matadorbet bid: matadorbet bid – matadorbet giris
Five Killer Quora Answers To Situs Togel Online situs Togel online – Weheardit.stream –
Why We Our Love For Item Upgrade (And You Should Also!) item level Upgrade
Find Out What Land Rover Discovery 4 Key Fob Replacement Tricks The Celebs Are Using land rover
discovery key (Suzette)
casino siteleri win: guvenilir casino siteleri – Casino Siteleri
дермантин цена дермантин цена .
buying tegretol
anonymous instagram story viewer anonymous instagram story viewer .
дерматин купить дерматин купить .
стоимость оформления перепланировки квартиры стоимость оформления перепланировки квартиры .
купить кожзам iskusstvennaya-kozha-dlya-mebeli-kupit.ru .
сколько стоит узаконить уже сделанную перепланировку https://stoimost-soglasovaniya-pereplanirovki-kvartiry.ru/ .
сколько стоит узаконить перепланировку квартиры http://stoimost-soglasovaniya-pereplanirovki-kvartiry.ru/ .
I’m gone to say to my little brother, that he should also go to see this blog on regular basis to obtain updated from most up-to-date information.
anonymous instagram story viewer anonymous instagram story viewer .
Casino Siteleri: casino siteleri win – Casino Siteleri
An Guide To Car Locksmith Near Me In 2023 car locksmith mobile
Порнуха
Приобретение диплома ПТУ с сокращенной программой обучения в Москве
Тут можно преобрести сейф для карабина купить сейф для оружия цены