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
%%
Feel free to surf to my blog post … 300Mg vegan cbd gummies
Way cool! Some very valid points! I appreciate you writing this article and
the rest of the site is also very good.
Also visit mmy blog: 人妻A片
ruonion darknet market updates 2022
магазины даркнета сайты даркнет ссылки
I do consider all of the ideas you have introduced in your post.
They’re really convincing and will definitely work. Nonetheless,
the posts are very short for starters. May you please prolong them a little
from subsequent time? Thanks for the post.
This paragraph provides clear idea in favor of the new users
of blogging, that genuinely how to do blogging and site-building.
Also visit my blog – โบด๊อก คาสิโน
%%
my site; doors Repairs
cannazon market darknet tor dark web
tor market links 2022 cannahome link
black market websites tor marijuana dark web
%%
Feel free to visit my web blog: Cbd Sweets benefits
the onion directory black market url deep web
dark markets czech republic buds express
%%
Feel free to visit my homepage: sexdolls for sale (Beth)
Thanks on your marvelous posting! I actually enjoyed reading it, you might be a great author.
I will be sure to bookmark your blog and will come back sometime
soon. I want to encourage you continue your great job,
have a nice evening!
THC gummies paris isolate is pure THC,
without various other cannabinoids.
0day onion wired darknet markets
What’s up Dear, are you actually visiting this web page regularly, if so after that you will definitely
get fastidious knowledge.
%%
my web blog :: Double glazing Glass replacement
https://thecracks.net/
74cd785c74 gesoty
%%
Feel free to visit my web page … sexdolls For sale
dark net market list reddit 0day onion
%%
Take a look at my webpage; Door repairs romford
dark web sites name list vice city market darknet
access the dark web reddit current darknet market list
%%
my web-site … leather Jackets with hood
%%
my page: bifold doors Lewisham
%%
Here is my web-site – upvc window repair near me
You actually make it seem so easy with your presentation but I find this topic to be actually something that I think
I would never understand. It seems too complicated and very broad for me.
I am looking forward for your next post, I’ll try to get
the hang of it!