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
Hello I am so excited I found your blog page, I really found you
by accident, while I was searching on Askjeeve for something else, Regardless I am here now and would just like to say thanks for a
incredible post and a all round interesting blog (I also love the theme/design), I don’t have time to browse it all at the minute but I have saved
it and also added in your RSS feeds, so when I have time I will be back to read much
more, Please do keep up the fantastic work.
Wow that was strange. I just wrote an incredibly long comment but after I
clicked submit my comment didn’t show up. Grrrr…
well I’m not writing all that over again. Regardless,
just wanted to say fantastic blog!
Сколько стоит диплом высшего и среднего образования и как его получить?
prednisone 50: cheap prednisone – where can i get prednisone
tor marketplace dark web search engine https://mydarknetmarketlinks.com/
I couldn’t resist commenting. Well written!
https://cytpremium.com/# п»їcytotec pills online
It’s remarkable to visit this web site and reading the views of all friends on the topic of this article,
while I am also keen of getting familiarity.
Диплом техникума купить официально с упрощенным обучением в Москве
amoxicillin 250 mg price in india: amoxil com pharm – generic amoxicillin
Medicine information leaflet. What side effects can this medication cause?
where can i get chlorpromazine pill
Actual information about medication. Read information here.
For hottest information you have to pay a visit internet and on web I found this site as a finest
web site for latest updates.
Как избежать рисков при покупке диплома колледжа или ПТУ в России
It’s appropriate time to make some plans for the longer term and it’s time to be happy.
I have learn this put up and if I may just I wish to recommend you few attention-grabbing issues or tips.
Perhaps you can write subsequent articles relating to this article.
I want to learn even more things approximately it!
Диплом вуза купить официально с упрощенным обучением в Москве
It’s truly a great and helpful piece of info.
I’m satisfied that you simply shared this useful info with us.
Please stay us up to date like this. Thank you for sharing.
tor market links tor markets links https://mydarknetmarketlinks.com/
It’s remarkable in favor of me to have a site, which is good in support of my know-how.
thanks admin
If you remain in the Phoenix area and wish to enhance your online presence, you ought to certainly look into Phoenix SEO services Digitaleer Phoenix SEO & Web Design