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
Официальная покупка диплома ПТУ с упрощенной программой обучения
paladiny.ru/forummess.dwar.php?TopicID=28716
Как получить диплом техникума с упрощенным обучением в Москве официально
erection pills that work: cheap drugs – online drugstore
9 Lessons Your Parents Taught You About Daftar Akun Togel Resmi daftar Akun togel resmi
ed pills online pharmacy cheap drugs buy prescription drugs online without
14 Smart Ways To Spend Your The Leftover
Psychiatrist Near Me Private Budget Private adhd Psychiatrist
кассовый чек организации
10 Things That Your Family Taught You About Replacement Key For Renault Trafic Van replacement key For renault Trafic van
Appreciating the hard work you put into your blog and in depth information you provide.
It’s good to come across a blog every once in a while
that isn’t the same old rehashed material. Excellent read!
I’ve saved your site and I’m including your RSS feeds to my Google
account.
Good post. I learn something new and challenging on blogs I stumbleupon everyday.
It will always be interesting to read content from other writers
and use a little something from other websites.
The Key Smith For Cars Awards: The Most Sexiest, Worst, And Strangest Things We’ve Ever Seen locksmith For Cars
Who’s The World’s Top Expert On Portable Electric Wheelchair?
Lightweight Power Chair
Wow! At last I got a webpage from where I know how to genuinely
take helpful facts concerning my study and knowledge.
I got this site from my buddy who shared with me regarding
this website and now this time I am visiting this
site and reading very informative articles or reviews here.
mexican rx online: mexico pharmacy – mexico pharmacies prescription drugs
Купить диплом магистра оказалось возможно, быстрое обучение и диплом на руки
pharmacy medications cheap medication ways to treat erectile dysfunction
Private ADHD Strategies That Will Change Your Life private adhd assessment glasgow cost (baidubookmark.com)
Ищете грузоперевозки Новосибирск Абакан? Мы предложим вам выгодные условия для доставки.
Hello, I read your new stuff daily. Your writing style is awesome, keep it up!
canadian online drugs cheap medication erection problems
The Steve Jobs Of Private ADHD Meet Your Fellow Private ADHD Enthusiasts.
Steve Jobs Of The Private ADHD Industry private adhd assessment ireland adult
ed meds online pharmacy cheap meds dog antibiotics without vet prescription
buying prescription drugs in mexico mexican pharmacy pharmacies in mexico that ship to usa
Great beat ! I would like to apprentice at the same time as you amend your website, how could i
subscribe for a blog web site? The account helped me a acceptable deal.
I have been a little bit acquainted of this your broadcast provided shiny clear concept
I was able to find good information from your blog articles.
online shopping pharmacy india: Best Indian pharmacy – cheapest online pharmacy india
Легальная покупка диплома о среднем образовании в Москве и регионах
St. Thomas the Apostle Boston Highway, London Borough of Ealing Stained glass Moira Forsyth made stained glass for the youngsters’s chapel and small panels on the west finish of the church, “Suffer Little Youngsters”, which was made in Arts and Crafts model.
Recent drug developments. Drug guide provided.
buy diflucan online without prescription
Medicine overview available. Pill information provided.