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
See What Nissan Key Replacement Tricks The Celebs Are Using nissan key replacement
Cheap Levitra online: Levitra 20mg price – Levitra 20 mg for sale
What You Must Forget About Making Improvements To Your Walking Machine For Desk shop
Repairing Upvc Windows 10 Things I’d Love To Have Known Earlier upvc
window Repair (sciencewiki.Science)
What Freud Can Teach Us About Double Bed Bunk Beds For Adults double Bed with bunk
Why We Do We Love Leeds Door Panels (And You
Should Also!) upvc door repairs meanwood
Why Is Trusted Online Shopping Sites For Clothes So Famous?
Carrigan Ottoman 33 Inch
10 Wrong Answers To Common Upvc Windows Repair Questions Do You
Know The Right Answers? repair upvc windows (Marcus)
http://cialist.pro/# Tadalafil Tablet
What Is Pornstars On Onlyfans And How To Use It
subscription
10 Misconceptions Your Boss Shares Concerning
Boat Accident Law leitchfield Boat accident lawsuit
https://levitrav.store/# Buy generic Levitra online
Viagra Tablet price: Buy generic 100mg Viagra online – Generic Viagra for sale
https://cenforce.pro/# Purchase Cenforce Online
You’ll Never Guess This Replacement Mini Key Fob’s
Tricks replacement mini key fob (Christine)
Are You Confident About Glass Window Repair?
Do This Test nearby
Wow, this paragraph is nice, my younger sister is analyzing these things, so I am going to tell her.
10 Things That Everyone Doesn’t Get Right About Mazda 3
Key Replacement Mazda Key (Promarket.In.Ua)
https://kamagra.win/# Kamagra Oral Jelly
See What Four Wheeled Electric Scooter Tricks The
Celebs Are Using Four Wheeled electric Scooter
buy valtrex cheap
Buy generic 100mg Viagra online: viagras.online – Buy generic 100mg Viagra online
Wow, superb blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is great, as well as the content!
You’ll Never Guess This 4 Wheel Motorized Scooter’s Secrets mobility scooter 4 Wheel
How Do You Know If You’re Are Ready To Work From Home Jobs Online Side Jobs From Home
10 Unexpected All CSGO Skins Tips Gamma 2 Case
The 3 Biggest Disasters In Avon Skin So Soft Dry Oil Spray Original History avon Skin so soft dry oil spray original
What’s The Job Market For Cerebral Palsy Attorney Professionals?
attorney
Do You Think Shopping Online Site Clothes Be The Next Supreme Ruler Of The
World? Shimmering Gold Pigment Powder
Kamagra 100mg price: kamagra.win – Kamagra Oral Jelly