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
Solar best energy plans australia (Merry) is clean. Gas plans are a lot simpler.
The 10 Scariest Things About Wood Burner Stoves Uk wood burner stoves – Curt,
You’ll Never Guess This 60 40 Fridge Freezer Integrated’s Tricks 60 40 fridge Freezer integrated
20 Trailblazers Leading The Way In Best Kids Bunkbeds Kids Furniture
Могу ли я сделать бронирование от имени другого https://www.odessamama.net/forum.php?mod=viewthread&tid=303338&extra= лица?
Могу ли я работать с рестораном напрямую?
https://rybelsus.shop/# rybelsus coupon
ozempic online ozempic generic ozempic coupon
What is New U Life SOMADERM Transdermal
Gel.
Also visit my website … euphoria mushroom gummies
What is New U Life SOMADERM Transdermal Gel.
My web blog; functional mushroom gummies
Официальная покупка диплома вуза с сокращенной программой обучения в Москве
Hi there, just wanted to say, I enjoyed this post. It was funny.
Keep on posting!
My web blog – privately owned centers
You’ll Never Guess This Sleeper Sectional With Chaise’s Tricks sleeper sectional With chaise
They have items to sell or solutions to provide.
Here is my web-site :: euphoria Mushroom Gummies
There are over 60 energy providers in Texas.
Feel free to surf to my website Bookmarks
Respect to article author, some fantastic information.
Feel free to surf to my web-site :: child care service raleigh
How To Get More Benefits Out Of Your Auto Folding Mobility Scooter Near Me auto Fold Mobility scooter uk, socialmphl.com,
В магазине сейфов предлагают сейфы простые сейфы купить в москве
Thanks for helping out, wonderful information.
Check out my web site … daycare arlington va
The intelligence is the best completely dry herb vape I have actually attempted.
Here is my homepage: Bookmarks
Mobile vaporizers make use of batteries as a power source.
Here is my page canadian plant based meat companies
ozempic ozempic ozempic
The vapor and flavor quality is likewise wonderful.
Check out my homepage Bookmarks
The average cost to move an electric meter of $675.
Feel free to visit my homepage … energy efficient tax credit 2022 form
Hi, just wanted to say, I liked this blog post. It was practical.
Keep on posting!
Feel free to surf to my web site; work from home
This vaporizer has a number of distinct features.
My homepage; Bookmarks
I leave a comment when I like starting a child care business
post on a blog or if I have something to contribute to the discussion. Usually it’s a
result of the passion displayed in the article I
browsed. And on this article JS 依照元素出現數量排序 – 馬老師 雲端研究室.
I was actually excited enough to drop a
thought 😛 I do have a couple of questions for you if it’s
allright. Could it be just me or does it look as if like some of the comments look
like they are coming from brain dead individuals? 😛 And, if you
are posting at additional sites, I’d like to keep up with everything fresh you have
to post. Would you list the complete urls of all your social sites like your twitter feed, Facebook page
or linkedin profile?
You’ll Never Be Able To Figure Out This Upvc Door Doctor Near Me’s Tricks Door Doctor
Simply put, solar energy is energy from the sun.
Here is my webpage Bookmarks
It’s time to look into energy comparison.
Here is my web page Bookmarks
We have actually aided 285 customers discover lawyers today.
My web page; how to get a personal lawyer