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
Somebody necessarily help to make severely articles I’d state.
This is the very first time I frequented your website page and so far?
I amazed with the research you made to create this particular put up incredible.
Magnificent job! https://Www.alna.sk/index.php/component/k2/item/53?start=0
Hi there! This article couldn’t be written much better!
Looking through this post reminds me of my previous roommate!
He constantly kept talking about this. I most certainly will
forward this information to him. Pretty sure he’s going to have a great read.
Thanks for sharing!
my homepage 서귀포출장마사지
https://gajweor.pixnet.net/blog/post/157629430
программа 1с купить программа 1с купить .
программа 1с купить программа 1с купить .
1с бухгалтерия купить 1с бухгалтерия купить .
Very good post. I definitely love this site.
Keep it up!
выведение из запоя воронеж стационар выведение из запоя воронеж стационар .
I seriously love your site.. Very nice colors & theme.
Did you develop this site yourself? Please reply back as I’m trying to
create my own personal website and want to find
out where you got this from or exactly what the theme is named.
Many thanks!
Известковые растворы используются почти всегда при внутренних работах, главными преимуществами данного материала являются комфорт и.
Take a look at my blog post: https://fishkaremonta.by/catalog/shtukaturka_zharostoykaya/
Indian Cert Pharm: Best online Indian pharmacy – indian pharmacy
best canadian online pharmacy https://mexicaneasypharm.com/# Mexican Easy Pharm
п»їlegitimate online pharmacies india
canadian online pharmacy https://canadianmdpharm.com/# canadian pharmacy ltd
indian pharmacy paypal
программа 1с купить http://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513/ .
настройка 1с бухгалтерии цена настройка 1с бухгалтерии цена .
canada drug pharmacy https://mexicaneasypharm.shop/# Mexican Easy Pharm
buy medicines online in india
Hi there, this weekend is fastidious in support of me, since
this occasion i am reading this impressive informative article here at my
house.
Hello, I read your new stuff regularly. Your story-telling style is witty, keep it up!
1с бухгалтерия купить 1с бухгалтерия купить .
https://naveridbuy.exblog.jp/35659090/
reputable mexican pharmacies online: Mexican Easy Pharm – Mexican Easy Pharm
Urban Nightlife 부산유흥
1с бухгалтерия купить 1с бухгалтерия купить .
настроить 1с бухгалтерия цена https://svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513 .
Best Indian pharmacy: Indian Cert Pharm – IndianCertPharm
medicine in mexico pharmacies: best online pharmacies in mexico – Mexican Easy Pharm
I every time spent my half an hour to read this blog’s
articles everyday along with a mug of coffee.
1с предприятие купить программу http://www.svstrazh.forum24.ru/?1-3-0-00000323-000-0-0-1737110513/ .
Outdoor Bar Furniture Showing Entertaining Easy 오피
legitimate canadian pharmacy online: Canadian Md Pharm – canadianpharmacy com