I have this array of hashes that was created when a did a API call and ran it through JSON.parse:
{
"results": [
{
"zip": "08225",
"city": "Northfield",
"county": "Atlantic",
"state": "NJ",
"distance": "0.0"
},
{
"zip": "08221",
"city": "Linwood",
"county": "Atlantic",
"state": "NJ",
"distance": "1.8"
}
]
}
I am trying to get all the zipcodes out of each object and put them into an array:
zipcode_array = Array.new
I have tried the following code:
locations.each do |zipcode|
zipcode_array.push(['results'][i]['zip'])
end
I would like my final output to be:
zipcode_array = ["08225", "08221"]
Anyone have any tips on what I'm missing?
Array.newyou can do[ ]in Ruby to make a new, empty array.Array.newis saved for special occasions likeArray.new(10,0)when you want an array with a particular pre-defined size and (optional) default.