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
Легальная покупка диплома о среднем образовании в Москве и регионах
пин ап зеркало: пин ап казино – pinup-kazi.ru
Nine Things That Your Parent Taught You About Key Fob Repair Service key fob repair service
пинап казино: пин ап казино официальный сайт – пин ап казино официальный сайт
вавада онлайн казино: вавада казино зеркало – вавада казино
I never realized how easy it is to fix minor issues myself until I read some guides online phone repair
Everything is very open with a clear explanation of the issues.
It was truly informative. Your website is very useful.
Thank you for sharing!
пин ап казино: pinup – pinup kazi
jackpoker играть онлайн
jackpoker играть бесплатно
I don’t know if it’s just me or if everybody else encountering problems with your
site. It appears as if some of the written text within your
content are running off the screen. Can someone else
please provide feedback and let me know if this
is happening to them as well? This could be a problem with my web browser because I’ve had this
happen previously. Cheers
пин ап кз: pin up казино – pin up казино
Bracelets usually snap on the wrist with a hook or clasp.
https://pinup-kazi.ru/# пин ап казино официальный сайт
how to get cheap betnovate
hello!,I really like your writing very so much! proportion we keep up a correspondence more about your article on AOL?
I need an expert on this house to solve my problem.
May be that is you! Taking a look ahead to see you.
A proactive technique to security can prevent losses that far surpass the expense of employing guards TreeStone Security Services
Unquestionably imagine that that you said. Your favourite reason seemed to be at the web the easiest factor to take into accout of.
I say to you, I certainly get irked while other folks consider concerns
that they plainly don’t know about. You controlled to hit the nail upon the top and also defined out the whole thing with no need side-effects , folks can take
a signal. Will likely be back to get more.
Thank you
darknet drug links dark website darknet sites
Как не попасть впросак при покупке диплома колледжа или ПТУ в России
кухни на заказ от производителя – Эксклюзивные проекты кухонь по доступным ценам.
vavada
Диплом техникума купить официально с упрощенным обучением в Москве
Medicines information for patients. Cautions.
trazodone 50 mg directions
Actual about medicament. Read here.
Купить диплом магистра оказалось возможно, быстрое обучение и диплом на руки
pinup: пин ап казино – pinup-kazi.kz
вавада онлайн казино: vavada-kazi.ru – vavada-kazi.ru
The Prisoner in the American Revolution, p.
http://pinup-kazi.ru/# pinup-kazi.ru
Has anyone else turned their garage into a living space? I got fantastic ideas from Revive Renovations bathroom remodeling that made it all
pinup-kazi.kz: pinup kazi – пин ап казино онлайн