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
What To Say About Marc Jacobs Crossbody Bag To Your Boss marc jacobs handbag Outlet
11 Creative Methods To Write About Shopping Online Uk Clothes Portable 4K Blu-Ray
Burner (vimeo.com)
cheapest cenforce: order cenforce – cenforce for sale
cheap viagra Cheapest place to buy Viagra Cheap generic Viagra online
https://viagras.online/# sildenafil 50 mg price
What’s The Job Market For Veleco Uk Professionals Like?
veleco uk (Alice)
Excellent beat ! I wish to apprentice while you amend your website, how can i subscribe for a blog website? The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear idea
What’s The Current Job Market For Double Glazed Window Repairs Professionals Like?
double glazed Window repairs
An In-Depth Look Back: How People Talked About Birth Defect Law 20 Years Ago
Littlefield Birth defect lawsuit
Truck Crash Lawyer Tips From The Top In The Industry Canby Truck Accident Law Firm
9 Lessons Your Parents Taught You About Double Glazed Windows Near Me replacement double glazed windows
New porn on views
https://levitrav.store/# Levitra 20 mg for sale
The 15 Things Your Boss Would Like You To Know You’d Known About
Citroen C4 Key Fob Replacement citroen relay Key fob
Hi to all, how is all, I think every one is getting more from this site,
and your views are nice designed for new users.
20 Trailblazers Lead The Way In Vauxhall Corsa Key Fob Replacement vauxhall Key fob
Your Family Will Thank You For Getting This Designer Handbags Brands designer Handbags list
Ten Window Repair Near Me-Related Stumbling Blocks You Shouldn’t Post On Twitter lock
You’ll Be Unable To Guess Window Repair Near Me’s Tricks window repair near me
The Next Big Thing In The Repair Double Glazed Window Industry Window Doctor Near Me
See What Glass Repair Leeds Tricks The Celebs Are Making Use Of
glass repair leeds
order cenforce: Purchase Cenforce Online – Cenforce 100mg tablets for sale
online tadalafil canada
17 Reasons Why You Should Ignore Washers Dryer Combo washers dryer Combo
Pretty section of content. I just stumbled upon your weblog and in accession capital to assert that I acquire in fact enjoyed account your blog posts. Any way I?ll be subscribing to your augment and even I achievement you access consistently quickly.
http://levitrav.store/# Generic Levitra 20mg
Cialis 20mg price in USA cialist.pro Tadalafil Tablet
cialis for sale buy cialis overseas cheapest cialis
I all the time used to study paragraph in news papers but now
as I am a user of net thus from now I am using net for articles or reviews, thanks to web.
Need Inspiration? Look Up Injury Lawyers dover injury Lawsuit