我想摆脱散列中每个属性中的value: <value>
键值。并且像这样:"total_interactions": 493.667
下面是不正确的格式,后面是我希望在json中实现的预期良好格式。
{
"3": {
"total_interactions": {
"value": 493.667
},
"shares": {
"value": 334
},
"comments": {
"value": 0
},
"likes": {
"value": 159.66666666666666
},
"total_documents": 6
},
"4": {
"total_interactions": {
"value": 701
},
"shares": {
"value": 300
},
"comments": {
"value": 0
},
"likes": {
"value": 401
},
"total_documents": 1
}
}
我希望它是这样的:
{
"3": {
"total_interactions": 493.6666666666667,
"shares": 334,
"comments": 0,
"likes": 159.66666666666666,
"total_documents": 6
},
"4": {
"total_interactions": 701,
"shares": 300,
"comments": 0,
"likes": 401,
"total_documents": 1
}
}
这是应该执行此操作但不起作用的代码。没有任何影响。不确定有什么问题
# the result_hash variable is the first hash with value: <value>
result_hash.each do |hash_item|
hash_item.each do |key,value_hash|
if( !value_hash.nil? )
value_hash.each do |k,v|
hash_item[key] = v
end
end
end
end
答案 0 :(得分:2)
db_subset <- structure(list(created_utc = c(1430438400L, 1430438410L,
1430438430L,
1430438455L, 1430438470L, 1430438480L)), .Names = "created_utc",
class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6"))
答案 1 :(得分:1)
#t.__orderedIndex
之后:
hash = {"3"=>{"total_documents"=>6, "comments"=>{"value"=>0}, "total_interactions"=>{"value"=>493.667}, "shares"=>{"value"=>334}, "likes"=>{"value"=>159.666666666667}},
"4"=>{"total_documents"=>1, "comments"=>{"value"=>0}, "total_interactions"=>{"value"=>701}, "shares"=>{"value"=>300}, "likes"=>{"value"=>401}}}
hash.each do |k,v|
v.each do |k2, v2|
if v2.is_a?(Hash) && v2["value"]
hash[k][k2] = v2["value"]
end
end
end
答案 2 :(得分:1)
如果你不想改变你的初始哈希<div class="row">
<div class="columns large-12">
<ul class="small-block-grid-2 medium-block-grid-3 large-block-grid-4">
<li>
<a href="http://127.0.0.1/makine/canon-powershot-elph-310-hs/" title="Canon PowerShot Elph 310 HS">
<img src="http://i.imgur.com/fwHd8zf.png" class="attachment-kucuk wp-post-image" alt="Canon_PowerShot_Elph_310_HS_428369_i0">
<h4>Canon PowerShot Elph 310 HS</h4>
</a>
</li>
<li>
<a href="http://127.0.0.1/makine/canon-powershot-s120/" title="Canon PowerShot S120">
<img src="http://i.imgur.com/HLuFBv0.png" class="attachment-kucuk wp-post-image" alt="Canon_PowerShot_S120_890378">
<h4>Canon PowerShot S120</h4>
</a>
</li>
<li>
<a href="http://127.0.0.1/makine/canon-1d-x/" title="Canon 1D X">
<img src="http://i.imgur.com/BlX05lN.png" class="attachment-kucuk wp-post-image" alt="Canon_EOS_1D_X_466261_i0">
<h4>Canon 1D X</h4>
</a>
</li>
<li>
<a href="http://127.0.0.1/makine/canon-7d-mark2/" title="Canon 7D Mark2">
<img src="http://i.imgur.com/DB3cfLX.png" class="attachment-kucuk wp-post-image" alt="_3962017">
<h4>Canon 7D Mark2</h4>
</a>
</li>
<li>
<a href="http://127.0.0.1/makine/canon-7d/" title="Canon 7D">
<img src="http://i.imgur.com/lOpicbY.png" class="attachment-kucuk wp-post-image" alt="Canon EOS 7D">
<h4>Canon 7D</h4>
</a>
</li>
<li>
<a href="http://127.0.0.1/makine/canon-70d/" title="Canon 70D">
<img src="http://i.imgur.com/GT0t4eo.png" class="attachment-kucuk wp-post-image" alt="Canon-eos-70-d-fotograf-makinesi">
<h4>Canon 70D</h4>
</a>
</li>
<li>
<a href="http://127.0.0.1/makine/canon-6d/" title="Canon 6D">
<img src="http://i.imgur.com/y2wRSZx.png" class="attachment-kucuk wp-post-image" alt="Canon 6D">
<h4>Canon 6D</h4>
</a>
</li>
<li>
<a href="http://127.0.0.1/makine/canon-ef-50mm-f1-8-ii/" title="Canon EF 50mm f/1.8 II">
<img src="http://i.imgur.com/5l2GjRG.png" class="attachment-kucuk wp-post-image" alt="Canon_EF_50mm_f18">
<h4>Canon EF 50mm f/1.8 II</h4>
</a>
</li>
</ul>
</div></div>
,你可以这样做:
h