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
mexican border pharmacies shipping to usa: online pharmacy in Mexico – medicine in mexico pharmacies
If you are going for most excellent contents like myself, just pay a visit this web site every day because it
provides feature contents, thanks
Nicely put, Thank you!
For most up-to-date information you have to visit internet
and on world-wide-web I found this site as a most excellent web site for latest updates.
indian pharmacy paypal: india online pharmacy – Online medicine order
pet meds without vet prescription canada CIPA approved pharmacies canadian pharmacy meds
buy prescription drugs from india Generic Medicine India to USA top 10 online pharmacy in india
ריחניות ותמרוקים לאמבט מושלם. כמו כן, אזור מולטימדיה הכולל טלוויזיית HD, מקרן קול חזק. לעתים, ניתן למצוא אף מטבחון עם פינת בהתעכבויות מיותרות. כמו כן האתר מציע לכם את הבנות היפות והשוות ביותר שתוכלו לדמיין מה גם שהכל אמיתי ומאומת ע”י האתר בצורה קפדנית דירה דיסקרטית בת ים
https://indianpharmgrx.com/# pharmacy website india
This Is The Complete Guide To Best Sex Toys For.Men the best sex toys for men (Huey)
https://canadianpharmgrx.xyz/# canadian pharmacy world
fantastic points altogether, you simply won a new reader.
What could you suggest about your submit that you
made some days ago? Any sure?
Can I just say what a relief to uncover someone who actually
knows what they’re discussing online. You actually realize
how to bring an issue to light and make it important.
A lot more people ought to check this out and understand this side of your
story. It’s surprising you’re not more popular since you most certainly have the
gift.
canadian pharmacy cheap Pharmacies in Canada that ship to the US canada discount pharmacy
the canadian pharmacy canadian pharmacy uk delivery canadian pharmacy review
mexican drugstore online Pills from Mexican Pharmacy mexican drugstore online
Вывод из запоя
pharmacy rx world canada: List of Canadian pharmacies – ed meds online canada
This is my first time pay a quick visit at here and i
am actually happy to read all at alone place.
india online pharmacy: indian pharmacy delivery – india pharmacy mail order
Hello! I could have sworn I’ve been to this site before but after browsing through some of the post I realized it’s new too
me. Nonetheless, I’m definitely glad I found it and I’ll be bookmarking and checking back often!
Look at my webpage … Dojarz praca Francja
I’ll immediately grab your rss as I can not find your e-mail
subscription hyperlink or newsletter service.
Do you’ve any? Kindly allow me realize in order that I may just subscribe.
Thanks.
chaqueta dortmund
great points altogether, you just gained a emblem new reader.
What may you suggest about your put up that you simply made some days in the past?
Any sure?
Hello and welcome. I’m Lauren Campuzano. My friends say it’s not therapeutic for me but what I like doing
is kayaking and I’ve been doing it for a while.
My wife and I live in New Hat. Interviewing is his profession but he plans on changing in which.
Отличный сайт! Всем рекомендую, качественный и быстрый ремонт техники! ремонт айфонов спб
Howdy! I realize this is kind of off-topic but I needed to ask.
Does operating a well-established website like yours take a massive amount work?
I am completely new to writing a blog however I do write in my journal everyday.
I’d like to start a blog so I can share my own experience and feelings
online. Please let me know if you have any recommendations or
tips for brand new aspiring bloggers. Thankyou!
Hi there! Do you know if they make any plugins to protect
against hackers? I’m kinda paranoid about losing
everything I’ve worked hard on. Any suggestions?
https://jeneri.ru/beg-i-potenciya.html
mexican pharmacy Mexico drugstore mexico drug stores pharmacies