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
nolvadex vs clomid does tamoxifen cause weight loss tamoxifen therapy
17 Signs To Know You Work With Side By Side Fridge Freezer Under Counter small side by side Refrigerator freezer
http://finasteride.store/# order cheap propecia without prescription
Are You Sick Of Double Glazing In Leeds? 10 Inspirational Sources That Will
Revive Your Love For Double Glazing In Leeds french door Repair leeds,
http://www.meiyingge8.com,
https://lisinopril.network/# lisinopril 15 mg tablets
cipro pharmacy buy generic ciprofloxacin ciprofloxacin
This History Behind Bunk Beds Will Haunt You Forever! Bunk beds sales (telegra.Ph)
buy misoprostol over the counter cytotec buy online usa buy cytotec over the counter
Used Mobility Scooters For Sale Near Me Tools To Help You Manage
Your Daily Lifethe One Used Mobility Scooters For Sale Near Me
Trick Every Person Should Be Able To mobility scooters for sale Near me
buy cytotec over the counter buy cytotec buy cytotec
lisinopril 10mg lisinopril 20 mg india zestril 20 mg price canadian pharmacy
https://finasteride.store/# propecia cost
What Is Cost Of A Replacement Car Key And How To Utilize
What Is Cost Of A Replacement Car Key And How To Use Damaged
15 Up-And-Coming ADHD Private Assesment Bloggers
You Need To Check Out private adhd assessment cost uk
tamoxifen cyp2d6: nolvadex pct – should i take tamoxifen
Wonderful beat ! I would like to apprentice while you amend your web site, how can i subscribe for a blog website?
The account aided me a acceptable deal. I had been tiny bit acquainted
of this your broadcast offered bright clear idea
buy tamoxifen: nolvadex online – tamoxifen estrogen
tamoxifen citrate tamoxifen buy where can i buy nolvadex
Online Shopping Website In London Tools To Ease Your
Everyday Lifethe Only Online Shopping Website In London Trick Every Person Should
Learn Online Shopping website in london
Terrific postings, Many thanks.
I visited various sites except the audio feature
for audio songs current at this web page is genuinely wonderful.
I like the helpful info you provide in your articles.
I’ll bookmark your blog and check again here frequently.
I am quite sure I’ll learn a lot of new stuff right here! Best of
luck for the next!
9 Things Your Parents Teach You About Situs Alternatif Gotogel situs alternatif gotogel
What Is It That Makes Memory Foam Mattresses Double So Popular?
Mattress double price
It Is The History Of Mental Health Check Up In 10
Milestones mental health assessment report (Martina)
I was recommended this website by my cousin. I’m not sure whether this post is written by him as no one
else know such detailed about my difficulty.
You’re amazing! Thanks!
What Ford Key Replacement Experts Want You To Learn Ford ka key replacement
Twenty Myths About Self Emptying Vacuum: Busted
self-emptying robot vacuum (agriexpert.kz)
Also visit my web blog; sex on facebook
Childrens Double Bunk Bed Explained In Less Than 140 Characters Double bunk beds With mattress