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
Electric Fireplace Wall Mounted Isn’t As Tough As You Think electric fireplaces wall
mounted (Tiffiny)
Normally I do not read post on blogs, but I wish to say that this write-up very pressured
me to check out and do so! Your writing style has been amazed me.
Thank you, very nice post.
https://rybelsus.shop/# rybelsus pill
Как оказалось, купить диплом кандидата наук не так уж и сложно
buy semaglutide online buy semaglutide online semaglutide online
Профессиональный сервисный центр сервисный центр телефонов ремонт телефонов
You’ll Be Unable To Guess Best Leather Sofa’s Secrets best leather sofa (Thelma)
ozempic generic: ozempic coupon – ozempic
buy viagra online
10 Sites To Help Learn To Be An Expert In Audi A4 Car Key Replacement
Audi advance key
I’m really impressed with your writing skills as well as with the layout on your blog.
Is this a paid theme or did you modify it yourself?
Anyway keep up the excellent quality writing, it is rare to see a nice blog
like this one today.
It’s an awesome piece of writing in support of all the
online people; they will take benefit from it I
am sure.
В магазине сейфов предлагают где купить сейф стоимость сейфа
5 Tools Everyone Involved In Double Glazed Sash Window Industry Should Be Utilizing Sash Windows
Five Killer Quora Answers On Situs Terpercaya Situs Terpercaya,https://Social4Geek.Com/,
30 Inspirational Quotes About Social Anxiety Symptoms Anxiety breathing symptoms
I am really thankful to the holder of this
web site who has shared this fantastic post at at this time.
https://ozempic.art/# ozempic coupon
https://ozempic.art/# ozempic cost
It’s an awesome article for all the web viewers; they will obtain benefit from
it I am sure.
20 Things Only The Most Devoted ADHD Assessment UK Private Fans Are Aware Of private Adhd assessment somerset
https://ozempic.art/# ozempic
The 10 Most Terrifying Things About Link Togel link togel (Vernell)
Genuinely no matter if someone doesn’t understand afterward its up to
other visitors that they will assist, so here it occurs.
https://rybelsus.shop/# semaglutide cost
15 Amazing Facts About Three Wheel Pushchair You’ve Never Seen three Wheel Pushchairs
нарколог на дом в краснодаре нарколог на дом в краснодаре .
Your method of telling everything in this piece of writing is genuinely good, every
one be able to without difficulty be aware of it,
Thanks a lot.
You’ll Never Guess This Small Sectional Sofa With Chaise’s Tricks Small Sectional Sofa With Chaise
вывод из запоя капельница ростов вывод из запоя капельница ростов .