Jimmy
Jimmy

Reputation: 12487

Returning specific value of javascript object

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?

Upvotes: 1

Views: 80

Answers (1)

Aoke
Aoke

Reputation: 43

You want the first value of the breakdown array not the array itself, right?

const metric = r2.report.data.map(obj => obj.breakdownTotal[0]);

Try this?

Upvotes: 2

Related Questions