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
An impressive share! I’ve just forwarded this onto
a colleague who was conducting a little homework
on this. And he actually ordered me breakfast because I stumbled upon it for him…
lol. So allow me to reword this…. Thank YOU for the meal!!
But yeah, thanks for spending time to talk about this issue here on your web site.
Hi there to every one, the contents existing at this
web page are in fact amazing for people knowledge, well, keep up the nice work fellows.
The Best Slot Developers Tricks For Changing Your
Life best slot Developers (http://www.gongjuilbo.kr)
Medicine information. What side effects can this medication cause?
buy cheap zoloft pills
Everything trends of medicines. Get information now.
Hi! Do you know if they make any plugins to assist with SEO?
I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good results.
If you know of any please share. Appreciate it!
Why No One Cares About Video Slots Best Payouts Slots (Maps.Google.Com.Ua)
Why You’ll Want To Find Out More About Veterans Disability Case veterans Disability lawyers
Excellent, what a weblog it is! This weblog gives useful facts to
us, keep it up.
You’ll Never Be Able To Figure Out This Double Glaze Repair Near Me’s Tricks double glaze repair Near me
Why Is Upvc Windows Luton So Popular? Window Repairs
Hello, I enjoy reading all of your post. I like to write a little comment to support you.
What’s The Job Market For Triple Pushchair Professionals Like?
Triple pushchair
The Reason Demo Sugar Is So Beneficial During COVID-19 Slot Demo Sugar Crush
купить диплом института http://www.ukr-diplom.ru/ .
What Slot Strategies Is Your Next Big Obsession top Jackpot slots
8 Tips To Increase Your Slot Technology Game Evoplay Slots tournaments
Is Your Company Responsible For The Slot Design Budget? 12 Best
Ways To Spend Your Money bonus round Slots
This Week’s Most Popular Stories About Foldable Pram Foldable Stroller
The salon best waiting visit one of the varieties massage, is what we do. What is an body to body massage interested in everyone. russian massage is the art of giving for bliss. You willsurprised to that,what ocean enjoyment can get to know from adopting massage. In massage studio body to body massage masseurs will make erotic bodywork massage.
How is it done, and is there something exotic? We will tell you all about him that you wanted to know |Our sensual massage is visited not only by men but also by women, and also by couples. You necessarily want to use only this infinitely … Our а task this is to please women and men magical voluptuous finnish massage. Special approach to all yours wishes and standards.
The sexy women our the spa will give you an unforgettable experience. The salon is a place of rest and relaxation. Here’s a Hawaiian Lomi Lomi massage, as though, and relaxation, exert influence on defined area naked body, what can help couple relax. Your best stop choice not on one masseuse, choose two girls! Choose by appearance, both professional and professional skills!
We in Gotham we recommend stunning placement with convenient design. All of these quarters apply be you you are staying with us secretly.
We have a showroom in Downtown. Girls Isabel –
japanese massage parlour
online aviator game http://www.aviator-crash-game.ru/ .
play aviator game online aviator-crash-game.ru .
Hi! Do you use Twitter? I’d like to follow you if that would
be ok. I’m definitely enjoying your blog and look forward to new updates.
15 Best Pinterest Boards Of All Time About Slot Rewards
top slots (https://yourbookmark.stream/story.php?title=5-things-that-everyone-is-misinformed-about-in-regards-to-certified-slots)
aviator play online https://aviator-crash-game.ru/ .
Where Will What CSGO Cases Have Butterfly Knives Be 1 Year
From In The Near Future? spectrum 2 case
14 Misconceptions Common To Uk Online Shoe Shopping
Websites Easy Care Shag Rug
20 Things You Must Be Educated About Slot Experiences 3D slots
aviator game online aviator game online .
20 Fun Informational Facts About Accident Legal accident law firms (Marina)
You’ll Be Unable To Guess Motorcycle Accident Case’s Tricks
motorcycle accident – Frank –