updating array based on symbols Ruby -
how 1 update array based on symbols?
data = [] string = "hello" if( !data.include? string ) count += 1 data.insert(-1, { label: string, value: count, }) else #logic change count value if string encountered again end
i thinking of finding index string lies , delete insert updated values @ index. right approach?
just use find
match, provided 1 in array. can use select
multiple matches. after update count
as example taken out of context , contains errors, i've taken liberty make more complete example.
data = [] strings = ["hello", "bye", "hello", "hi"] strings.each |string| hash = data.find{ |h| h[:label] == string } if hash.nil? data << {label: string, value: 1} else hash[:value] += 1 end end
Comments
Post a Comment