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
buy priligy max pharm: cheap priligy – cheap priligy
priligy max pharm: priligy – max pharm
This Is The Advanced Guide To Retro American Fridge Freezer Retro American Fridge Freezers (Istartw.Lineageinc.Com)
Peculiar article, totally what I was looking for.
What’s up, everything is going sound here and ofcourse
every one is sharing facts, that’s really good, keep up writing.
Тут можно преобрести сейф для оружия купить сейф для оружия купить москва
where to buy amoxicillin over the counter: com pharm – amoxicillin over the counter in canada
amoxicillin 500 mg without a prescription: cheap amoxil – medicine amoxicillin 500mg
Тут можно преобрести сейфы под оружие купить сейф для карабина
I think the admin of this website is genuinely working hard
for his web site, since here every information is quality based information.
amoxicillin 500 mg where to buy: buy amoxicillin 500mg – how much is amoxicillin prescription
amoxicillin 500 mg brand name: cheap amoxil – amoxicillin medicine
amoxicillin 500: Amoxicillin buy online – where to buy amoxicillin pharmacy
Your method of telling everything in this post is truly fastidious, every one be capable of easily be aware of it, Thanks a lot https://carryu.com/
Hello, i think that i saw you visited my website thus i came
to “return the favor”.I am trying to find
things to enhance my web site!I suppose its ok to use some
of your ideas!!
dark web links darknet drug market best darknet markets
Узнай все о варикоцеле 1 степени варикоцеле 3 степени
Узнай все о степени варикоцеле двустороннее варикоцеле
I visited various web sites except the audio quality
for audio songs present at this web site is truly superb.
Nice post. I learn something totally new and challenging on blogs I stumbleupon every day.
It’s always helpful to read through articles from other authors and
use something from other web sites.
I like the valuable info you provide in your articles.
I’ll bookmark your weblog and check again here
regularly. I’m quite certain I’ll learn plenty of new stuff right here!
Best of luck for the next!
Also visit my blog; cammatch
I visited multiple websites except the audio quality
for audio songs present at this site is in fact fabulous.
Узнай все о варикоцеле яичка лапароскопия варикоцеле
buy dapoxetine online: buy priligy max pharm – buy priligy
Узнай все о двустороннее варикоцеле признаки варикоцеле
These are genuinely impressive ideas in on the topic of blogging.
You have touched some pleasant factors here. Any way keep up
wrinting.
20 Important Questions To ASK ABOUT Electric Stove Fireplace
Before You Buy Electric Stove Fireplace buy electric stove
darknet market lists dark websites deep web search
Полезная информация как купить диплом о высшем образовании без рисков
iluzeia.flybb.ru/viewtopic.php?f=2&t=271
buy tizanidine without insurance