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
cost of amoxicillin 30 capsules: amoxil – buying amoxicillin online
What’s up to all, how is all, I think every one is getting more from this site, and your views are fastidious for new visitors.
Medication information sheet. Short-Term Effects.
naltrexone or vivitrol
Actual trends of medicines. Get now.
buy cheap clomid without dr prescription: clomid online – cost of generic clomid price
Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed
in Yahoo News? I’ve been trying for a while but I never seem to get there!
Thank you
15 Best Documentaries On Link Togel Link Togel resmi – e-bookmarks.Com,
This is a very good tip especially to those fresh to the blogosphere.
Brief but very precise info… Many thanks for
sharing this one. A must read article!
Have you ever thought about including a little bit more than just your articles?
I mean, what you say is valuable and all. However
think about if you added some great photos or videos to give
your posts more, “pop”! Your content is excellent but with
images and video clips, this website could definitely be one of the best in its field.
Awesome blog!
Узнай все о рецидив варикоцеле варикоцеле
Приветствую. Подскажите, где почитатьполезные статьи о недвижимости? Пока нашел – https://baltimkom.ru
5 prednisone in mexico: prednisoneraypharm – prednisone online pharmacy
Всем привет! Может кто знает, где найтиразные блоги о недвижимости? Пока нашел – https://baltimkom.ru
I’m not that much of a internet reader to
be honest but your blogs really nice, keep it up!
I’ll go ahead and bookmark your site to come back down the
road. Cheers
Всем привет! Подскажите, где почитатьполезные статьи о недвижимости? Пока нашел – https://baltimkom.ru
Приветствую. Подскажите, где почитатьразные статьи о недвижимости? Пока нашел – https://baltimkom.ru
where to buy generic clomid for sale generic clomid can i buy cheap clomid online
It’s appropriate time to make a few plans for the long
run and it’s time to be happy. I’ve learn this post and if I could I desire
to counsel you some interesting things or advice. Maybe you can write subsequent articles regarding this article.
I desire to read even more things about it! Co168
Узнай все о степени варикоцеле варикоцеле причины
Узнай все о варикоцеле слева степени варикоцеле
Всем привет! Может кто знает, где почитатьполезные статьи о недвижимости? Сейчас читаю – https://baltimkom.ru
Узнай все о варикоцеле левого яичка клиника варикоцеле
dark websites dark market list bitcoin dark web
Всем привет! Может кто знает, где почитатьразные статьи о недвижимости? Пока нашел – https://baltimkom.ru
My relatives every time say that I am killing my time here at web, except I know I am getting experience all the time by reading such fastidious content.
cheap clomid without rx: clomid – can you buy generic clomid tablets
Wall Mantel Electric Fireplace Tools To Streamline Your Daily Lifethe One Wall Mantel Electric Fireplace Trick That Every Person Should Learn wall mantel electric fireplace – Terry –
cost of amoxicillin prescription buy amoxil online amoxicillin where to get
cost generic clomid pill: generic clomid – how can i get clomid without insurance
На сегодняшний день самым эстетичным методом протезирования считаются безметалловые коронки. При этом под словом «керамика» подразумеваются самые разные химические соединения безметалловые коронки на зубы Волгоград
amoxicillin 500mg capsules: amoxil com pharm – medicine amoxicillin 500mg