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
Купить диплом ВУЗа России
Как не попасть впросак при покупке диплома колледжа или ПТУ в России
Купить диплом Барнаул
amoxicillin order online no prescription http://priligymaxpharm.com/# buy priligy
Купить диплом Барнаул
over the counter amoxicillin canada http://amoxilcompharm.com/# amoxicillin 775 mg
I think the admin of this website is in fact working hard
in favor of his site, since here every information is quality based information.
Как не попасть впросак при покупке диплома колледжа или ПТУ в России
amoxicillin 500 mg where to buy: Amoxicillin buy online – amoxicillin 500 mg brand name
amoxil generic https://prednisoneraypharm.com/# order prednisone on line
prednisone over the counter uk
Hey there just wanted to give you a quick heads up.
The words in your content seem to be running off the screen in Internet explorer.
I’m not sure if this is a formatting issue or something
to do with internet browser compatibility but I thought I’d post to let you know.
The design and style look great though! Hope you get the problem solved soon. Many
thanks
Узнайте, как приобрести диплом о высшем образовании без рисков
If some one wants expert view concerning blogging and
site-building after that i suggest him/her to visit this website, Keep up the
nice job.
Как получить диплом техникума официально и без лишних проблем
Very quickly this site will be famous amid all blog visitors, due
to it’s pleasant content
Стоимость дипломов высшего и среднего образования и процесс их получения
can you get clomid without dr prescription: generic clomid – how to get generic clomid online
Priligy tablets: buy priligy max pharm – buy priligy
Ten Ways To Build Your Double Glazing Bromley Empire bromley door Company; clashofcryptos.trade,
can we buy amoxcillin 500mg on ebay without prescription http://amoxilcompharm.com/# amoxicillin 500 mg capsule
Охотно принимаю. На мой взгляд, это актуально, буду принимать участие в обсуждении. Вместе мы сможем прийти к правильному ответу. Я уверен.
Они прикладываются к портфолио и демонстрируются вместе с https://fgosonline.ru/olimpiady/po-angliyskomu-yazyku/angliyskij-yazyk-8-klass/ ним аттестационной комиссии. повторное первое, второе, третье место, место лауреата или участника.
amoxicillin 500 mg where to buy https://clomidrexpharm.com/# where can i get cheap clomid for sale
Как получить диплом техникума официально и без лишних проблем
Букмекерская компания Зенит известна своими выгодными коэффициентами и акциями для игроков. Платформа работает с акцентом на прозрачность и доверие.
Всё, что нужно знать о покупке аттестата о среднем образовании без рисков
I’m gone to say to my little brother, that he should also pay a quick visit this webpage on regular basis to get updated
from hottest news.
Купить диплом в России, предлагает наша компания
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
cost of generic celebrex prices