I have hash data (originally in json) and an array of selected hash keys:
jsondata ='{"1":
{"name": "Tax Exempt"},
"2":
{"name": "Tax on Purchases"},
"3":
{"name": "Tax on Sales"},
"4":
{"name": "Service Tax"}
}'
parseddata = JSON.parse(jsondata);
selectedtax = ["2","3"]
My code maps the keys and returns the value of the hash that exist in the array. Here is the code:
selectedtaxdetails = Array.new
parseddata.map do |key,value|
if selectedtax.include? key
selectedtaxdetails << value
end
end
Output of selectedtaxdetails is:
[{"name": "Tax on Purchases"},{"name": "Tax on Sales"}]
How can I improve my code?
selected_tax_detailsis easier to readselectedtax = ["2",3"]