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
нарколог на дом http://pelsh.forum24.ru/?1-8-0-00000125-000-0-0-1730729877 .
электрокарниз цена электрокарниз цена .
Medicine information. What side effects can this medication cause?
where to get aricept without prescription
Best trends of medication. Read information here.
Good day! Do you use Twitter? I’d like to follow you if that would be ok.
I’m undoubtedly enjoying your blog and look forward to new updates.
вызов нарколога на дом вызов нарколога на дом .
нарколог на дом срочно https://masa.forum24.ru/?1-16-0-00002626-000-0-0-1730730156/ .
нарколог на дом срочно http://www.ya.7bb.ru/viewtopic.php?id=14601 .
карнизы для штор с электроприводом карнизы для штор с электроприводом .
Wow, this article is pleasant, my sister is analyzing
these things, so I am going to let know her.
Also visit my web blog :: Copper peptide serum
you’re truly a excellent webmaster. The web site loading pace is
amazing. It sort of feels that you’re doing any unique trick.
Moreover, The contents are masterwork. you have done a great
activity on this matter!
Also visit my blog post – IronRods
darknet markets 2024 dark web link https://darknetmarketstore.com/ – tor market url
карнизы с электроприводом карнизы с электроприводом .
cheap priligy: max pharm – dapoxetine online
regardless from that, choose whether you are a classic slot, a rich farm or fighting the undead,
you are waiting fascinating https://sweet-bonanza-uk.org/!
выезд нарколога на дом http://www.belbeer.borda.ru/?1-6-0-00000754-000-0-0-1730730058/ .
вызов нарколога на дом ya.7bb.ru/viewtopic.php?id=14601 .
Hello, yeah this post is genuinely fastidious and I have learned lot of
things from it about blogging. thanks.
Also visit my homepage – walet69 link alternatif
нарколог на дом срочно нарколог на дом срочно .
max pharm: max pharm – Priligy tablets
order prednisone from canada: raypharm – buy prednisone from canada
1win registro
buy priligy: dapoxetine price – priligy
http://www.webcocktail.ru — Наш сайт предлагает разнообразие кухонь на заказ для любого бюджета.
частный нарколог на дом spilkuvannya.rolevaya.com/viewtopic.php?id=66 .
врач нарколог на дом платный https://spilkuvannya.rolevaya.com/viewtopic.php?id=66/ .
нарколог на дом анонимно http://www.flanrp.rolevaya.com/viewtopic.php?id=146 .
Hi! This is kind of off topic but I need some guidance from an established blog.
Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty quick.
I’m thinking about creating my own but I’m not sure where to begin. Do you
have any points or suggestions? Cheers
Here is my blog :: omegle alternatives
dark market list darkmarket list https://darknetmarketstore.com/ – darknet market list
Excellent, what a weblog it is! This weblog presents helpful data to
us, keep it up.
Visit my page :: Best accident lawyers in Phoenix
выезд нарколога на дом http://www.dubna.myqip.ru/?1-5-0-00000282-000-0-0-1730730082 .