生成哈希数组的铁路地图方法
Rail map method to generate array of hash
我有一个哈希数组,我正在使用映射生成一个哈希数组。当我的数组有两个值时,我可以生成 has 数组,但我想使用一个数组生成相同类型的结果。
我有一个数组是:
b = [
{
id: 1,
State: "Andhra Pradesh",
Rural_Oct_16_Index_Final: 137.5,
Rural_Oct_17_Index_Provisional: 143,
Rural_Inflation_Rate_in_percentage: 4,
Urban_Oct_16_Index_Final: 131.7,
Urban_Oct_17_Index_Provisional: 135.6,
Urban_Inflation_Rate_in_percentage: 3,
Combined_Oct_16_Index_Final: 135.4,
Combined_Oct_17_Index_Provisional: 140.3,
Combined_Inflation_Rate_in_percentage: 3.6,
created_at: "2018-08-30T05:01:58.000Z",
updated_at: "2018-08-30T05:01:58.000Z"
},
{
id: 2,
State: "Bihar",
Rural_Oct_16_Index_Final: 135.7,
Rural_Oct_17_Index_Provisional: 138.7,
Rural_Inflation_Rate_in_percentage: 2.2,
Urban_Oct_16_Index_Final: 128.2,
Urban_Oct_17_Index_Provisional: 131.4,
Urban_Inflation_Rate_in_percentage: 2.5,
Combined_Oct_16_Index_Final: 134.6,
Combined_Oct_17_Index_Provisional: 137.6,
Combined_Inflation_Rate_in_percentage: 2.2,
created_at: "2018-08-30T05:01:58.000Z",
updated_at: "2018-08-30T05:01:58.000Z"
}
]
我使用这段代码:
b.map do |el|
{ y: el['Rural_Oct_16_Index_Final'], label: el['State'] }
end
我得到这个结果:
[
{
y: 3.6,
label: "Andhra Pradesh"
},
{
y: 2.2,
label: "Bihar"
}
]
所以我的问题是当我的数组只有一个值时:
[
{
id: 1,
State: "Andhra Pradesh",
Rural_Oct_16_Index_Final: 137.5,
Rural_Oct_17_Index_Provisional: 143,
Rural_Inflation_Rate_in_percentage: 4,
Urban_Oct_16_Index_Final: 131.7,
Urban_Oct_17_Index_Provisional: 135.6,
Urban_Inflation_Rate_in_percentage: 3,
Combined_Oct_16_Index_Final: 135.4,
Combined_Oct_17_Index_Provisional: 140.3,
Combined_Inflation_Rate_in_percentage: 3.6,
created_at: "2018-08-30T05:01:58.000Z",
updated_at: "2018-08-30T05:01:58.000Z"
}
]
并且使用 map 方法我想要这样的结果:
[
{
y: 2.2,
label: "Rural_Inflation_Rate_in_percentage"
},
{
y: 128.2,
label: "Urban_Oct_16_Index_Final"
}
]
当我使用这段代码时:
b.map do |el|
{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" },
{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" }
end
当我删除逗号时出现此错误我得到一个没有错误的结果。
有什么方法可以使用 map 方法来完成这项工作。
b.flat_map do |el|
[{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" },
{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" }]
end
我有一个哈希数组,我正在使用映射生成一个哈希数组。当我的数组有两个值时,我可以生成 has 数组,但我想使用一个数组生成相同类型的结果。
我有一个数组是:
b = [
{
id: 1,
State: "Andhra Pradesh",
Rural_Oct_16_Index_Final: 137.5,
Rural_Oct_17_Index_Provisional: 143,
Rural_Inflation_Rate_in_percentage: 4,
Urban_Oct_16_Index_Final: 131.7,
Urban_Oct_17_Index_Provisional: 135.6,
Urban_Inflation_Rate_in_percentage: 3,
Combined_Oct_16_Index_Final: 135.4,
Combined_Oct_17_Index_Provisional: 140.3,
Combined_Inflation_Rate_in_percentage: 3.6,
created_at: "2018-08-30T05:01:58.000Z",
updated_at: "2018-08-30T05:01:58.000Z"
},
{
id: 2,
State: "Bihar",
Rural_Oct_16_Index_Final: 135.7,
Rural_Oct_17_Index_Provisional: 138.7,
Rural_Inflation_Rate_in_percentage: 2.2,
Urban_Oct_16_Index_Final: 128.2,
Urban_Oct_17_Index_Provisional: 131.4,
Urban_Inflation_Rate_in_percentage: 2.5,
Combined_Oct_16_Index_Final: 134.6,
Combined_Oct_17_Index_Provisional: 137.6,
Combined_Inflation_Rate_in_percentage: 2.2,
created_at: "2018-08-30T05:01:58.000Z",
updated_at: "2018-08-30T05:01:58.000Z"
}
]
我使用这段代码:
b.map do |el|
{ y: el['Rural_Oct_16_Index_Final'], label: el['State'] }
end
我得到这个结果:
[
{
y: 3.6,
label: "Andhra Pradesh"
},
{
y: 2.2,
label: "Bihar"
}
]
所以我的问题是当我的数组只有一个值时:
[
{
id: 1,
State: "Andhra Pradesh",
Rural_Oct_16_Index_Final: 137.5,
Rural_Oct_17_Index_Provisional: 143,
Rural_Inflation_Rate_in_percentage: 4,
Urban_Oct_16_Index_Final: 131.7,
Urban_Oct_17_Index_Provisional: 135.6,
Urban_Inflation_Rate_in_percentage: 3,
Combined_Oct_16_Index_Final: 135.4,
Combined_Oct_17_Index_Provisional: 140.3,
Combined_Inflation_Rate_in_percentage: 3.6,
created_at: "2018-08-30T05:01:58.000Z",
updated_at: "2018-08-30T05:01:58.000Z"
}
]
并且使用 map 方法我想要这样的结果:
[
{
y: 2.2,
label: "Rural_Inflation_Rate_in_percentage"
},
{
y: 128.2,
label: "Urban_Oct_16_Index_Final"
}
]
当我使用这段代码时:
b.map do |el|
{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" },
{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" }
end
当我删除逗号时出现此错误我得到一个没有错误的结果。
有什么方法可以使用 map 方法来完成这项工作。
b.flat_map do |el|
[{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" },
{ y: el['Rural_Inflation_Rate_in_percentage'], label: "Rural_Inflation_Rate_in_percentage" }]
end