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
Можно ли купить аттестат о среднем образовании, основные моменты и вопросы
https://klccestate.com/property/bangsar-bungalow-for-sale/
Thanks for the useful post. More like this at Helpful site
damansara heights house for sale
how to get clomid without insurance: clomid – where can i buy cheap clomid without prescription
What’s up, just wanted to mention, I enjoyed this post. It was practical.
Keep on posting!
Бесплатный информационный сервис, предоставляющий контент и справочную информацию о инвестировании.
инвестиция денег
A Intermediate Guide For Electric Fires For Media Wall media fireplace (Eric)
See What Shop Glass Repair Tricks The Celebs Are Using shop glass repair (https://telegra.ph)
purchase cytotec: cyt premium – Abortion pills online
More interestingly, US players will find some of the highest-paying
slot entertainment with an extensive section of Bitcoin progressive jackpot
games.
п»їplavix generic PlavixClo Best Price buy clopidogrel bisulfate
buying generic clomid without a prescription: clomid – clomid rx
https://stanproperty.com/bungalow/
Пошаговая инструкция по безопасной покупке диплома о высшем образовании
Cost of Plavix on Medicare: buy Plavix Clo – buy clopidogrel online
plavix best price: Plavix Clo – Clopidogrel 75 MG price
Как приобрести диплом о среднем образовании в Москве и других городах
buying land in florida
Clopidogrel 75 MG price: PlavixClo Best Price – plavix medication
lisinopril 20mg prices buy Lisinopril 1st buy Lisinopril online
Somebody necessarily assist to make seriously articles I might state.
This is the very first time I frequented your website page and
to this point? I surprised with the research you made to create
this particular put up incredible. Wonderful activity!
I enjoyed this read. For more, visit indian catering london
https://lisinopril1st.com/# lisinopril1st
You definitely made the point.
prinivil 25mg: lisinopril mexico – lisinopril 10 mg price in india
https://shopaiz.com/bungalow/
кухня на заказ – выбор для тех, кто хочет стильный и удобный интерьер.
agriculture land loan
Lisinopril 1st: buy Lisinopril 1st – buy Lisinopril 1st