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 Things You Don’t Know About Bmw Key Replacement bmw smart key
This Is The History Of Double Glazed Window Repairs Near
Me Double Glazed Window Repairs Near Me
One Boat Accident Litigation Success Story You’ll Never
Be Able To boat accident law firms
Truly plenty of excellent tips.
Greetings from Ohio! I’m bored to death at work so I decided to browse your website on my iphone during
lunch break. I really like the info you provide here and can’t wait to take a look when I get home.
I’m surprised at how quick your blog loaded on my mobile ..
I’m not even using WIFI, just 3G .. Anyways, great site!
Hi there, everything is going perfectly here and ofcourse every
one is sharing data, that’s truly fine, keep up writing.
10 Misconceptions Your Boss Holds About Erb’s Palsy Law Erb’s Palsy Attorneys
This Is The Advanced Guide To Sash Windows Repair double glazed Window repairs Near me
15 Unquestionably Reasons To Love Mesothelioma Lawsuits Asbestos settlement
семена растение кат купить
The Little-Known Benefits Of Bmw Car Key Cover bmw car key cover
Find Out What Automotive Locksmith Tricks The Celebs Are Using automotive locksmith cost; bybak.com,
How To Ship To Ireland From Uk Tools To Help You Manage
Your Daily Life How To Ship To Ireland From Uk Trick
Every Individual Should Learn How to Ship to ireland from Uk
Ten Sales Team Leadership That Will Improve Your Life
Sales Leadership
Bmw Key Fob 101″The Complete” Guide For Beginners Lost bmw keys
15 Semi Truck Settlement Benefits Everybody Should
Be Able To accidents
You’ll Never Guess This Window Repair Near Me’s Secrets Window repair
Near Me [m.042-527-9574.1004114.co.kr]
This Week’s Most Popular Stories About Sofa Sets For Sale Sofa Sets For Sale two
seater couch for sale (Buford)
Thanks for sharing your thoughts about Sight Care Supplement.
Regards
I absolutely love your blog and find almost
all of your post’s to be exactly what I’m looking for.
Does one offer guest writers to write content available for you?
I wouldn’t mind composing a post or elaborating on a few of the subjects
you write regarding here. Again, awesome blog!
buy canadian drugs: Certified Canadian Pharmacies – best canadian pharmacy to order from
Excellent article. Keep posting such kind of info on your page.
Im really impressed by it.
Hi there, You’ve done a fantastic job. I’ll definitely digg
it and for my part recommend to my friends. I am confident they will be benefited
from this website.
The Worst Advice We’ve Heard About Double Glazing Shops Near Me double glazing window replacement
How To Outsmart Your Boss On Leeds Windows And Doors Upvc Window Handle Repairs Leeds
In Which Location To Research Designer Handbags Online online
Who Is Responsible For The Upvc Window Repairs Budget?
12 Ways To Spend Your Money upvc window Repairs Near me
Unexpected Business Strategies That Helped Bunk Bed For Children To Succeed best bunk beds
8 Tips To Up Your Avon Skin So Soft Spray Game avon skin so soft spray,
Jared,
The Hidden Secrets Of Asbestos Litigation Asbestos Settlement
подготовительные курсы Беларусь