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
15 Surprising Facts About SEO Company UK Top content Marketing companies
https://cenforce.pro/# cheapest cenforce
http://viagras.online/# generic sildenafil
https://milkyway.cs.rpi.edu/milkyway/show_user.php?userid=6758750
You’ll Be Unable To Guess Window Repair Near Me’s Secrets window repair near me
Hey there, I think your website might be having
browser compatibility issues. When I look at your blog in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping.
I just wanted to give you a quick heads up!
Other then that, very good blog!
Where Do You Think Cut Key For Car Be 1 Year From In The Near Future?
near
Five Killer Quora Answers To Replacement Double Glazing Units Near
Me replacement double glazing units near me
It’s The Perfect Time To Broaden Your Cheapest 10kg Washing
Machines Options uk
Looking For Inspiration? Check Out Replacement Mini Key Fob mini cooper Replacement keys
See What Article Rewriter Ai Tricks The Celebs Are Utilizing Article Rewriter Ai
The Greatest Sources Of Inspiration Of Window Repair Near near By
Cialis 20mg price in USA buy cialis overseas Buy Tadalafil 10mg
The Most Hilarious Complaints We’ve Heard About Online Shopping Sites In Uk For Electronics White Deer Head Wall Mount
https://kamagra.win/# buy kamagra online usa
Where Will Online Part Time Work From Home Be
1 Year From This Year? Genuine Work From Home Jobs Uk (http://Www.Vvboard.Com.Cn)
Here is my page – find sex near me
What’s The Current Job Market For Electric Wall Mounted Fires Professionals Like?
electric wall mounted fires (qooh.me)
30 Inspirational Quotes On Double Glazed Units Near Me double glazing Unit
The 10 Scariest Things About Treadmill UK treadmill
Kamagra tablets kamagra oral jelly Kamagra tablets
You’ll Never Guess This Amazon Online Grocery Shopping Uk’s Tricks
amazon Online grocery Shopping uk (s1.backlinkindex.net)
cheapest cialis Cialis 20mg price in USA Generic Cialis without a doctor prescription
The Most Sour Advice We’ve Ever Seen About Double Over Double Bunk Beds For Adults Double Over Double Bunk Beds For Adults bunk Bed with a double (pattern-wiki.win)
Why No One Cares About Can You Earn CSGO Cases revolution Case [https://willysforsale.com]
Buy Vardenafil 20mg: Vardenafil price – Buy Vardenafil 20mg online
What Is Automatic Folding Mobility Scooters And Why Is Everyone Speakin’ About It?
lightweight Mobility scooter
5 Killer Quora Answers On Mazda 2 Key Mazda 2 Key
Check Out: How Injury Legal Is Gaining Ground, And How To Respond avon Injury attorney
buy Kamagra kamagra oral jelly super kamagra