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
ラブドール 中古During testing the owners were instructed to say a word corresponding to an object before showing the dog either the correct item or one that did not fit the label.An owner might say,
klcc land owner
‘klcc magnificent penthouse
Официальная покупка диплома вуза с сокращенной программой в Москве
Hello, i think that i saw you visited my website so i came
to “return the favor”.I’m attempting to find things to enhance
my web site!I suppose its ok to use some of your ideas!!
https://lisinopril1st.com/# lisinopril1st
bookmarked!!, I love your blog!
I am not sure where you’re getting your information, but great topic.
I needs to spend some time learning more or understanding more.
Thanks for excellent information I was looking for this info for my mission.
Пошаговая инструкция по официальной покупке диплома о высшем образовании
50 mg prednisone canada pharmacy: prednisone ray pharm – buy prednisone 1 mg mexico
Эвакуатор с манипулятором – универсальное решение для перевозки автомобилей. Он сочетает функции обычного эвакуатора и крана-манипулятора. Это позволяет решать сложные задачи по эвакуации. Манипулятор может поднимать и перемещать грузы в труднодоступных местах https://kak-cdelat.ru/inventar/210-detskaya-shvedskaya-stenka-svoimi-rukami-chertezhi.html
agriculture land for sale selangor
price is just one of the factor to buy penthouse
Предлагаем услуги профессиональных инженеров офицальной мастерской.
Еслли вы искали сервисный центр lg в москве, можете посмотреть на сайте: сервисный центр lg
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!
Excellent article on roof inspections! Regular checks can save homeowners a lot of money. For more tips on maintaining your roof, head over to roofing Contractor
cost cipro without a prescription
Hey there! I’ve been reading your weblog for a long time now and finally
got the courage to go ahead and give you a shout out from
Humble Tx! Just wanted to say keep up the good job!
can i buy clomid without rx: clomid online – where buy generic clomid tablets
Покупка школьного аттестата с упрощенной программой: что важно знать
buy cytotec over the counter: cytpremium – buy cytotec
buying land in florida
I’ll immediately grab your rss feed as I can not
find your e-mail subscription hyperlink or newsletter
service. Do you’ve any? Kindly allow me know in order that I may subscribe.
Thanks.
Полезные советы по безопасной покупке диплома о высшем образовании
Lisinopril 1st: cheapest Lisinopril – buy Lisinopril 1st
prefer luxury only penthouse in klcc
очерк об Cryptoboss casino, которое уникально тем что
принимает в оплату все криптовалюты мира
What’s up i am kavin, its my first occasion to commenting anywhere, when i read this
piece of writing i thought i could also make comment due
to this brilliant article.
Официальная покупка школьного аттестата с упрощенным обучением в Москве
Fantastic post but I was wanting to know if you could write a litte more on this subject?
I’d be very thankful if you could elaborate a little bit further.
Appreciate it!
Feel free to visit my page dementia housing
Bar Hopping 하이오피주소