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
My brother suggested I might like this web site. He was totally right.
This post actually made my day. You cann’t imagine simply how much time I had spent for this information! Thanks!
Как получить диплом о среднем образовании в Москве и других городах
Полезные советы по безопасной покупке диплома о высшем образовании
dvoinoi-brus.ru/kupit-attestat-za-11-klass-v-volzhskom.html
A youtube mp3 download tool is a free web application that allows
users to download videos from youtube.
matadorbet.bid matadorbet bid matadorbet.bid
Аттестат 11 класса купить официально с упрощенным обучением в Москве
Hello, I enjoy reading all of your post. I like to write a little comment to support
you.
9 Lessons Your Parents Taught You About Cheap Sleeper Sofas cheap sleeper sofas
deneme bonusu veren siteler yeni https://slot-tr.online/# slot tr online
Как официально купить диплом вуза с упрощенным обучением в Москве
The Reasons To Focus On Improving Renault Replacement Key Card renault trafic replacement
key (https://hikvisiondb.webcam)
купить диплом в великих луках [url=https://prema365-diploms.ru/]prema365-diploms.ru[/url] .
Powerful treatment is available when you ivermectin for dogs dosage chart from one of these pharmacies
I always emailed this blog post page to all my friends,
because if like to read it afterward my contacts will too.
I am regular visitor, how are you everybody? This piece of writing posted at
this website is really fastidious.
https://w1.livesydney.co/
ideal umzüge
Hi there, just wanted to mention, I loved this article.
It was practical. Keep on posting!
deneme bonusu veren siteler yerliarama.org deneme bonusu veren siteler betturkey betturkey.com deneme bonusu veren siteler yeni
10 Sites To Help You To Become A Proficient In Kia Sportage Key Fob 2013 Kia Rio
Key Fob Programming (Angerway64.Werite.Net)
Реально ли приобрести диплом стоматолога? Основные этапы
cheap inderal without a prescription
You’ll Never Be Able To Figure Out This Panels For Upvc Doors’s Tricks Panels For Upvc Doors
My programmer is trying to convince me to move
to .net from PHP. I have always disliked the idea because of the costs.
But he’s tryiong none the less. I’ve been using Movable-type on various websites for about a year and am worried about switching to another platform.
I have heard excellent things about blogengine.net.
Is there a way I can import all my wordpress content into it?
Any kind of help would be greatly appreciated!
slot oyunlar?: az parayla cok kazandiran slot oyunlar? – en kazancl? slot oyunlar?
Официальная покупка аттестата о среднем образовании в Москве и других городах
thisisyourreponguns.com
Как купить диплом о высшем образовании с минимальными рисками
tanphuockhanhvillage.vn/kupit-attestat-za-10-i-11-klassi.html
Как получить диплом техникума с упрощенным обучением в Москве официально
Можно ли купить аттестат о среднем образовании? Основные рекомендации
ultrabet bonus ultrabet giris ultrabet yeni giris 1125
prinz umzüge
Hello, this weekend is pleasant designed for me, since this time i am reading this impressive informative article here at my residence.
instagram anonymous story viewer storyinst.com .