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
Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
Мы предлагаем: ремонт ipad в москве на дому
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
15 Things You Didn’t Know About Smart Car Keys Smart Keys Cars
The 9 Things Your Parents Taught You About Car Key Programmer program
I loved as much as you will receive carried out right here.
The sketch is tasteful, your authored subject matter
stylish. nonetheless, you command get got an nervousness over that
you wish be delivering the following. unwell unquestionably come further formerly
again since exactly the same nearly a lot often inside case
you shield this increase.
https://xn--2i0bm4p0sf2wh7vdmsy.kr
slot casino siteleri: slot siteleri – yasal slot siteleri
https://sweetbonanza.network/# sweet bonanza mostbet
https://xn--2i0bm4pd1btzg9pkelcg77a.com
http://sweetbonanza.network/# sweet bonanza 90 tl
Hello and welcome. My name is Santo Cifuentes. Maryland
is your biggest I love most. Fishing is the thing she loves most of all.
She works as a payroll clerk but she plans on changing in which.
Have you ever considered about adding a little bit more than just your articles?
I mean, what you say is valuable and all. But just
imagine if you added some great photos or video clips
to give your posts more, “pop”! Your content is excellent but with
images and clips, this site could undeniably be one of the very best in its field.
Good blog!
Here is my web site … look at this web-site
See What Best Robot Vacuum Mop For Pet Hair Tricks The Celebs Are Using
best robot vacuum mop for pet hair
https://hp9.kr
The Top Kia Ceed Key Fob Replacement Gurus Can Do Three Things
2013 Kia optima key replacement
Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
Мы предлагаем: ремонт планшетов на дому
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
https://xn--2i0bm4p0sf2wh7vdmsy.net
How Much Can Mini Cooper Key Replacement Experts Earn? Mini cooper Key not working
Fastidious response in return of this question with
solid arguments and explaining all regarding that.
I’m amazed, I have to admit. Seldom do I encounter a blog that’s equally educative and engaging, and without
a doubt, you have hit the nail on the head. The issue is something that not enough men and
women are speaking intelligently about. Now i’m very happy I found this during my hunt for
something relating to this.
Hi! I just wanted to ask if you ever have any problems with hackers?
My last blog (wordpress) was hacked and I ended up losing a
few months of hard work due to no back up. Do
you have any methods to stop hackers?
Five Killer Quora Answers On L Shaped Sleeper Sofa l shaped Sleeper Sofa
Are Adjustable Clubs Inside Your Future? 수성구오피 (https://sv.rilpedia.org)
Профессиональный сервисный центр по ремонту планетов в том числе Apple iPad.
Мы предлагаем: сервис ipad москва
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
yeni slot siteleri: en cok kazandiran slot siteleri – deneme bonusu veren siteler
What’s up, just wanted to tell you, I loved this article.
It was helpful. Keep on posting!
Wonderful work! That is the kind of information that
are supposed to be shared across the net. Shame on the search engines for
now not positioning this put up upper! Come on over and
talk over with my site . Thanks =)
casino slot siteleri: deneme bonusu veren slot siteleri – slot siteleri bonus veren
Remarkable! Its really remarkable piece of writing, I have got much
clear idea about from this paragraph.
slot oyun siteleri: slot siteleri 2024 – guvenilir slot siteleri
https://medium.com/@dqvchristopherwhite824/%EB%84%A4%EC%9D%B4%EB%B2%84-%EB%B8%94%EB%A1%9C%EA%B7%B8%EC%99%80-%EC%B9%B4%ED%8E%98%EC%9D%98-%EC%B0%A8%EC%9D%B4%EC%A0%90%EA%B3%BC-%ED%99%9C%EC%9A%A9%EB%B2%95-cd856b093b00