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
%%
Here is my site … London Adhd Clinic
Excellent, what a web site it is! This web site presents valuable data to us,
keep it up.
Online betting has a number of advantages for both bookmakers and bettors.
Online betting sites permit people to place bets
on their favorite sports, as well as earn them money.
These sites offer many ways to deposit funds.
Also visit my webpage live – http://mgri-rggru.ru/ –
Thanks very interesting blog!
Check out my homepage; QQSutera
Aw, this was a really good post. Spending some time and actual effort
to produce a great article… but what can I say… I hesitate a whole lot and don’t manage to get anything done.
The popularity of online gambling has increased in the last decade.
In 1996, there were just fifteen websites. In 1997 there were over 200 websites.
Feel free to visit my site: betting
There are many reasons to get involved in betting on sports.
From the possibility of making a profit to the excitement and the chance to root for your favorite team in addition to the
overall experience.
Feel free to surf to my website Gambler
dark web search engines blackweb
Sports fans in New York are allowed to place bets
online starting January. 8 2022. Four major operators
have been granted licenses for mobile betting.
They will be able offer their services in a variety of ways.
My homepage: gambler
There are many benefits to playing at an online casino.
Signing up doesn’t require you to spend a lot of money.
Most online casinos offer free games, and players can test them
to determine what games they like the best.
Check out my web page – dealers
Wow that was unusual. I just wrote an really long comment but after I clicked
submit my comment didn’t appear. Grrrr… well I’m not writing all that
over again. Regardless, just wanted to say excellent blog!
%%
my homepage – Emergency Locksmith Near Me
%%
My homepage Cabin Beds Near Me
Greetings! Very helpful advice within this article!
It’s the little changes that will make the most significant changes.
Thanks a lot for sharing!
Here is my blog – chosungreen.softedu.co.kr
Online poker offers many advantages. One of them is that there are
no waiting lists, no second-hand smoke, and you can play any time you’d like.
You can also enjoy many different games.
Stop by my blog post :: dealers (incrediblepanels.com)
Betting on online sports betting gives bookmakers and bettors many benefits.
Apart from allowing gamblers to place bets on their favorite sports online betting
sites allow them to earn money. They provide a wide range
of options for depositing money.
My site: Casino – 20tools.com,
%%
my web blog :: Triple beds
%%
Here is my blog … Window Glass Replacement Manchester
Howdy! Would you mind if I share your blog with my facebook group?
There’s a lot of folks that I think would really enjoy your content.
Please let me know. Thank you
%%
my blog post :: Locksmiths Emergency
The Internet has opened up a variety of new opportunities for people who love
to play. The popularity of online poker has grown dramatically in recent years.
Feel free to surf to my web-site; gaming (Georgiana)
The Internet has created a wealth of new opportunities for those who love
poker. The popularity of online poker has increased significantly in recent
years.
Also visit my webpage: best
cvs prescription prices without insurance comfortis without vet prescription
Slots online are an increasingly popular pastime for millions of casino players online.
This thrilling and enjoyable type of gambling is a excellent way to pass the time or
to make extra money.
dark web search engines dark web sites links
can you mix viagra and sleeping pills – https://belviagra.com buy viagra online from uk
mexican pharmacy without prescription how to get prescription drugs without doctor
%%
my blog post :: best unisex Perfumes
There are a myriad of rules and regulations that govern online gambling.
For instance in the US, you cannot have gambling sites that are in your home country.
However, it could be set up elsewhere.
Feel free to visit my web-site: Sports
Online gambling has grown in popularity in the last decade.
In 1996, there were just fifteen websites. In 1997, there were more than 200 websites.
According to Frost & Sullivan, online gambling
generated $830 million in revenues in 1998.
Feel free to visit my page … blackjack