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
The No. 1 Question That Anyone Working In Sectional
Sofas For Sale Should Know How To Answer Oversized couch
http://canadaph24.pro/# my canadian pharmacy reviews
How To Explain Mesothelioma Lawyers To Your Mom asbestos case
mexican pharmaceuticals online: mexican pharmacy – buying from online mexican pharmacy
mexican rx online: Online Pharmacies in Mexico – reputable mexican pharmacies online
Wow, amazing weblog structure! How lengthy have you ever been blogging for?
you make running a blog glance easy. The total look of your web site is excellent, let alone the
content material!
https://mexicoph24.life/# buying from online mexican pharmacy
The Reasons You Should Experience Best CSGO Case Opening Sites
At Least Once In Your Lifetime case clutch (Leesa)
pharmacy in canada canadianpharmacyworld com canadian pharmacy com
5 Killer Quora Answers To Double Glazing Repairs
Leeds Glazing Repairs Leeds (Maps.Google.Cat)
http://indiaph24.store/# reputable indian online pharmacy
I’m really loving the theme/design of your website.
Do you ever run into any web browser compatibility issues?
A couple of my blog readers have complained
aboput my website not operating correctly in Explorer
but looks great in Opera. Do you have any suggestions to help fix this issue?
Here is my web page; Aizen Power Review
Get To Know One Of The Slots For Fun Industry’s Steve Jobs Of The Slots For Fun Industry Best Online slots
http://mexicoph24.life/# reputable mexican pharmacies online
Don’t Buy Into These “Trends” About Pvc Window Repairs upvc window repair
india pharmacy https://indiaph24.store/# buy medicines online in india
india online pharmacy
What Is New Audi Key And How To Utilize What Is New Audi Key And
How To Use audi key Copy
This Week’s Most Popular Stories About Double Glazed Windows Near Me Double Glazed
Windows Near Me Double Glaze
http://mexicoph24.life/# medicine in mexico pharmacies
20 Trailblazers Lead The Way In Birth Defect Attorney birth defect law firm
mail order pharmacy india http://indiaph24.store/# reputable indian online pharmacy
indian pharmacies safe
п»їbest mexican online pharmacies: purple pharmacy mexico price list – mexican mail order pharmacies
Great site ! I recommend everyone to watch it!escort paris
top online pharmacy india indian pharmacy online pharmacy india
buying prescription drugs in mexico online: mexico pharmacy – mexican pharmacy
Ten Myths About Volkswagen Key Replacement That Don’t Always
Hold how to get a new volkswagen key
indian pharmacy online http://indiaph24.store/# cheapest online pharmacy india
online shopping pharmacy india
See What Designer Handbags Brands Tricks The Celebs
Are Using designer handbags brands
top online pharmacy india Cheapest online pharmacy online pharmacy india
buying from online mexican pharmacy Online Pharmacies in Mexico mexican border pharmacies shipping to usa