Getting null value using map in Rails

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Getting null value using map in Rails



I am getting null value when I am using if condition inside a map. I have an array and I looping through that.



My first array is:


[
{
id: 5,
Vegetables: "Cabbage",
Area: 39,
Production: 695.33,
Year: 2014,
created_at: "2018-07-18T06:23:11.000Z",
updated_at: "2018-07-18T06:23:11.000Z"
},
{
id: 12,
Vegetables: "Bittergourd",
Area: 9.71,
Production: 67.25,
Year: 2014,
created_at: "2018-07-18T06:23:11.000Z",
updated_at: "2018-07-18T06:23:11.000Z"
},
.....
]



This is my array and and I using this code to loop:


ji1 = ["Cabbage","Bittergourd"]
hash_data = ji1.map do |col|

dataset = col.to_s.gsub("_"," ")
{
type:views,
legendText: dataset,
showInLegend: true,
dataPoints: b.reject{|x| x["Districts"]== "Bihar"}.map do |el|
if el["Vegetables"] == "Bittergourd"
{ y: el["Area"], label: el["Year"] }
end
end
}
end



In this example b is my array I am getting this value. I want a result like this:


[
{
type: "column",
legendText: "Cabbage",
showInLegend: true,
dataPoints: [
{
y: 9.7,
label: 2014
}



]
},
{
type: "column",
legendText: "Bittergourd",
showInLegend: true,
dataPoints: [
{
y: 39,
label: 2014
}

.....

]
}
]



When I am running my loop I am getting null value is there any way I can create a result like above using map function with my array.





I don't see where some of the information should be coming from. For instance, where is the variable view coming from in: type:views?
– bo-oz
1 hour ago


view


type:views





you can put anything it doesn't matter
– Nlay Singh
1 hour ago





Why don't you just loop through the hash and add an extra key to it on each iteration? This sample code doesn't make sense to me, so can't help you with the exact code, without seeing where the variables (like view) are being declared.
– bo-oz
1 hour ago





actual data is gist.github.com/nwoow/a5ce22fbae020925c2e52b0f7730c72a
– Nlay Singh
1 hour ago




1 Answer
1



Being 'b' the data you provided, and removing views since I don't know where that variable comes from... try this piece of code. I believe it achieves what you are trying to do


grouped_data = b.group_by{ |data| data[:Vegetables]}

grouped_data.map{ |vegetable, values|
dataset = vegetable.to_s.gsub("_"," ")
{

legendText: dataset,
showInLegend: true,
dataPoints: values.map { |value|
{ y: value[:Area], label: value[:Year] }
}
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

d9pr6lTjGzus v2lKCdd u3ElvqCvko,Le5I k4,Em IrPJvw,ric
EJcY Raon,ItMkIFtRDehhJz77tZ,XN,N8Ct vEKN fFtq2bhhV4

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham