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’s The Current Job Market For Mazda 2 Key Fob Replacement
Professionals Like? mazda 2 Key Fob
10 Things We All Do Not Like About Which CSGO Case Is The Most
Profitable cs20 case
20 Questions You Need To Ask About Double.Glazing Near Me Prior To Purchasing Double.Glazing Near Me fit
sildenafil online: Buy Viagra online cheap – Buy Viagra online cheap
It’s A Michael Kors Bag Success Story You’ll Never Remember brown
buy kamagra online usa kamagra oral jelly Kamagra Oral Jelly
5 Reasons To Consider Being An Online Shopping Online And 5 Reasons Not To Online Shopping
(Gigatree.Eu)
Kamagra 100mg price kamagra oral jelly Kamagra 100mg
You’ll Never Guess This Private Adhd Assessment Near Me’s Secrets Adhd assessment near me
Why We Why We Veleco Scooter (And You Should Too!) http://www.Mymobilityscooters.uk
Why You’re Failing At Personal Injury Law Farmville personal injury law firm
Generic Cialis price: buy cialis online – Tadalafil price
https://viagras.online/# Sildenafil Citrate Tablets 100mg
Hi, this weekend is fastidious designed for me, as this time i am reading this great educational post here at my house.
My webpage: mens golf shoes
The Step-By -Step Guide To Choosing Your Bean-To-Cup Machine Bean To Cup Machines
http://kamagra.win/# Kamagra tablets
cialis no prescription canada
If you wish for to get much from this post then you have to apply these methods to your
won weblog.
This History Behind Skin So Soft Dry Oil Spray Can Haunt You Forever!
avon So Soft
Levitra online pharmacy: Buy Vardenafil 20mg – Levitra 10 mg best price
Very quickly this web site will be famous amid all blogging visitors, due to it’s fastidious content
Cenforce 100mg tablets for sale: cheapest cenforce – cheapest cenforce
The People Nearest To Adult Toy For Men Tell You Some Big Secrets Nearest
The 10 Most Terrifying Things About Heavy Duty Electric Wheelchair
Heavy duty electric Wheelchairs
10 Things You Learned In Kindergarden They’ll Help You Understand Online Shopping
Sites Clothes Cheap breathable Patio Cover
The Biggest Sources Of Inspiration Of Best CSGO
Case To Open operation broken Fang case
10 Best Books On Saab Keys saab key battery Replacement
Cialis without a doctor prescription: Generic Tadalafil 20mg price – Tadalafil Tablet
10 Signs To Watch For To Look For A New France Online Shopping Sites Clothes Vimeo
7 Secrets About Birth Injury Case That No One Will
Tell You scottsboro birth Injury lawsuit