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
5 Killer Quora Answers On Online Shopping Uk
Women’s Clothing online shopping uk women’s clothing
I quite like reading a post that will make men and women think.
Also, thank you for allowing for me to comment!
Highly energetic post, I loved that a lot. Will
there be a part 2?
Look into my blog: tonic greens
The 10 Scariest Things About Good Online Shopping Sites Uk Good online shopping sites uk (https://m.ruael.com/member/login.html?nomemberorder&returnurl=http://vimeo.com/930937580)
Good post. I definitely love this site. Thanks!
The drink was initially bottled in inexperienced
glass bottles with crown caps. Throughout this process,
the drink loses a few of its sweetness and eventually resembles sparkling wine.
Elderberry sparkling wine is made from the
flowers of the black elderberry, also known as lilac in northern Germany, water, sugar, lemon slices and acid (tartaric or citric acid) or vinegar and adjustments style
and character during pure fermentation. On the following pages,
sixteen plastic and metallic drinking bottles have been tested.
The sizes of the drinking bottles are based mostly on the amount of protein shakes you devour.
Suitable for bottles of all sizes akin to: B.
200 ml, 250 ml, 330 ml, 500 ml, 750 ml or one thousand ml,
we provide a big number of colors in our in depth range
and naturally we not solely print glass water bottles along with your individual emblem.
It will likely be significant to match how typically such
a bottle may be reused, while those made of plastic, for instance, should be disposed
of after every use.
Lorem Ipsum has been the industry’s standard dummy text https://btelectricshop.es/dgmnh of type and scrambled it to make a type specimen book.
Guide To Slot Themes: The Intermediate Guide For Slot Themes Slot themes; http://inprokorea.com,
where to get generic cozaar tablets
10 Inspirational Graphics About Semi Truck Legal
Semi Truck Accident Lawsuits
For those who find social situations challenging, have irontech dollgone through difficult relationship experiences, or feel out of place in the regular dating scene
This is my first time visit at here and i am truly happy to
read everthing at single place.
Everyone loves what you guys tend to be up too. This sort of clever work and coverage!
Keep up the amazing works guys I’ve included you guys to my personal blogroll.
I believe that is among the such a lot significant information for
me. And i’m glad studying your article. But should observation on few general issues, The web site taste is wonderful, the
articles is actually nice : D. Just right task, cheers
Howdy! cvs pharmacy apply online excellent web page.
Learn What Veterans Disability Lawsuit Tricks The Celebs Are Utilizing
veterans disability law firm, Klara,
10 Easy Steps To Start Your Own Accident Case Business accident lawsuit
where to buy generic lexapro without insurance
Reviews of computer games, articles about game consoles
15 Shocking Facts About Best Online Shopping Sites
For Clothes That You Didn’t Know best Luxury online shopping sites uk
It’s perfect time to make a few plans for the
longer term and it’s time to be happy. I have learn this
put up and if I could I want to counsel you few fascinating issues or advice.
Perhaps you could write next articles relating to this article.
I want to learn even more things approximately it!
You’ll Never Guess This Double Glaze Repair Near Me’s Tricks
double glaze repair near me
Guide To Double Glazed Window Near Me: The Intermediate Guide In Double Glazed Window Near Me double glazed window Near me
What’s The Job Market For Uk Women’s Online Shopping Websites Professionals?
uk women’s online shopping websites
10 Things You Learned In Preschool To Help You Get A
Handle On Car Accident Litigation Car Accident Attorney
20 Quotes That Will Help You Understand Veleco Mobility veleco
cristal mobility scooter (Darwin)
magnificent points altogether, you simply gained a logo new reader.
What would you suggest in regards to your submit that you just made some days
in the past? Any sure?
10 Locations Where You Can Find Real Money Slots video slots – Levi –
I am sure this piece of writing has touched all the internet people, its really really
pleasant article on building up new website.
A Step-By-Step Guide To Slot Developers From Beginning To End Real Casino Slots