I have a script which returns this as "r2":
{
waitSeconds: 0,
runSeconds: 0,
report: {
data: [{
breakdown: [{}],
breakdownTotal: ["608674"],
day: 1,
month: 10,
name: "Mon. 1 Oct. 2018",
year: 2018,
}, {
name: "Tue. 2 Oct. 2018",
year: 2018,
month: 10,
day: 2,
breakdown: [{}],
breakdownTotal: ["123456"]
}, {
name: "Wed. 3 Oct. 2018",
year: 2018,
month: 10,
day: 3,
breakdown: [{}],
breakdownTotal: ["123456"]
}, {
name: "Thu. 4 Oct. 2018",
year: 2018,
month: 10,
day: 4,
breakdown: [{}],
breakdownTotal: ["123456"]
}, ],
elements: [{}],
metrics: [{}],
}
}
I'm trying to capture two things and return them in the format:
[X,Y,Z,ETC]
I've been able to grab the day using this code:
const days = r2.report.data.map(obj => obj.day);
However when I try and get breakdownTotal (i.e 608674)
const metric = r2.report.data.map(obj => obj.breakdownTotal);
It returns this:
[Array(1), Array(1), Array(1), Array(1)]
How can I get it to return the breakdownTotal values?
breakdownTotalis an array in your example data, so that seems correct. What are you trying to achieve?breakdownTotalis an array, you can doconst metric = r2.report.data.map(obj => obj.breakdownTotal[0]);to get them to be"608674"but that's assuming the value ofbreakdownTotalalways only has one value in the array, or do you just want the value of const to be"608674"?r2.report.data.map(obj => obj.breakdownTotal[0]);copy(r2)to copy the variable as formatted JSON into your clipboard. Otherwise you can logJSON.stringify(r2, null, 2)and then manually copy the output. This helps people understand the problem.