javascript - How to properly implement a hash-map/do I even need one -


i trying use chartsjs great graph of followers on time. have graph working using less efficient method.

data: row of dates (when each follow happened)

dates = ['mar 15', 'mar 16', 'mar 18', 'mar 18'] 

the graph takes array of dates , value @ each date.

the goal above following:

graph = [['mar 14','mar 15', 'mar 16', 'mar 17', 'mar 18'],[0,1,2,2,4]] 

so right now, program starts @ first date in graph , checks if there values in dates match, increments value stored.

after doing research, think should using hash-map make more efficient, can't wrap head around how i'd it.

i don't particularly need code snippets in answer, quick explanation of should doing.

any , appreciated.

edit: according comments, data coming rails backend. instead of manipulating in frontend better set in rails.

for example, can do

follow_count_on_dates = @follows.map {|f| f.created_at.change(hour: 0)}.each_with_object(hash.new(0)) {|follow_date,hash| hash[follow_date.strftime("%b %d")]+=1 } 

which hash looks

{"mar 19"=>32, "mar 05"=>2, "apr 04"=>1, "may 01"=>2, "apr 06"=>1, "apr 21"=>1, "apr 22"=>1, "may 04"=>3} 

which can inject dom , use in js


explanation

first map @follows activerecord object created_at property has exact same hour, can work days only. returns array of dates lots of repeated ones. chained call each_with_object iterate using hash initializes 0 (the object send in) setting key created_at object parsed strftime("%b %d") , incrementing value inside (which starts @ 0 passing hash.new(0) every time see equal date.

by way, shouldn't use strftime year value well?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -