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
https://mexicanpharm24.cheap/# mexican online pharmacies prescription drugs
generic ed drugs
What’s The Point Of Nobody Caring About Integrated Fridge
Freezer With Water Dispenser plumbed fridge freezer with water and ice dispenser
Where Is Best Male Masturbation Toy Be 1 Year From In The Near Future?
best male masturbation toys
Guide To Situs 4d: The Intermediate Guide For Situs 4d situs 4D
5 People You Should Be Getting To Know In The Four Seater Chesterfield Sofa Industry antique chesterfield
ed treatment drugs http://pharm24.pro/# erectile dysfunction medications
I do not even know how I ended up here, but I thought this post was good.
I don’t know who you are but definitely you’re going to a famous blogger if you aren’t already ;
) Cheers!
my web-site; https://www.cucumber7.com/
7 Things About Accident Injury Attorneys Near Me You’ll Kick Yourself For Not Knowing attorney Accident lawyer
purple pharmacy mexico price list Mexican pharmacy ship US best online pharmacies in mexico
капельница от запоя на дому люберцы капельница от запоя на дому люберцы .
Everything You Need To Learn About Best Bunk Bed bunk Bed Uk
7 Simple Changes That Will Make A Big Difference In Your Autofold Mobility Scooter automatic folding mobility Scooters
Heya i’m for the first time here. I found this board and I find It really
useful & it helped me out a lot. I hope to give
something back and help others like you helped me.
Сервисный центр предлагает замена аккумулятора lenovo ideapad s210 touch замена системых охлаждения lenovo ideapad s210 touch
вывод из запоя люберцы москва вывод из запоя люберцы москва .
indian pharmacies safe Indian pharmacy online reputable indian pharmacies
20 Trailblazers Setting The Standard In Folding Treadmills treadmills That fold Up
mexican drugstore online mexico pharmacy cheap medicine in mexico pharmacies
капельница от запоя на дому люберцы капельница от запоя на дому люберцы .
One Key Trick Everybody Should Know The One Audi A5 Replacement Key
Trick Every Person Should Know audi replacement key service near
me – Gogogobookmarks.com,
вывод из запоя цены на дому москва вывод из запоя цены на дому москва .
вывод из запоя в москвае nazalnyj.ru .
Your Worst Nightmare Concerning Espresso Machines Relived coffee espresso machine
вывод из запоя люберцы цены вывод из запоя люберцы цены .
Сервисный центр предлагает поменть стекло huawei ascend d1 quad xl замена дисплея huawei ascend d1 quad xl
нарколог на дом люберцы нарколог на дом люберцы .
Pretty! Ƭhis was an incredibly wondcerful article.
Τhanks fօr supplying tһiѕ info.
Also visit my web site; discuss
Medicines information leaflet. Drug Class.
can i get generic inderal without insurance
Best about medication. Get information here.
https://mexicanpharm24.cheap/# purple pharmacy mexico price list
buy ed drugs
değer kaybı talep beyanı
This is a good tip especially to those fresh to the blogosphere.
Brief but very precise info… Appreciate your sharing this one.
A must read post!