0

How can I access 'line' data from the given object? This is the link to api -https://sportsbook.draftkings.com/api/odds/v1/leagues/3/offers/gamelines.json

{
    "events": [{
        "id": 153502679,
        "name": "Green Bay Packers @ Chicago Bears",
        "homeTeamName": "CHI Bears",
        "awayTeamName": "GB Packers",
        "startDate": "2019-09-06T00:20:00.0000000Z",
        "offers": [{
            "id": "1-2160521227",
            "label": "Point Spread",
            "outcomes": [{
                "id": "1-2568051855",
                "label": "GB Packers",
                "line": "+3.5000",
                "oddsAmerican": "-110",
                "oddsDecimal": 1.9100,
                "oddsFractional": "10/11",
                "participant": "GB Packers"
            },

I am getting

<td>{{data.name }}</td>
        <td>{{data.homeTeamName }}</td>
        <td>{{data.awayTeamName}}</td>
        <td>{{data.startDate }}</td>
        <!--<td>{{data.offers[1].label }}</td>-->
        <td>{{data.offers.outcomes[2].line }}</td>

I tried the above code to access line in outcomes object by it give me below error

ERROR TypeError: Cannot read property '2' of undefined
1

3 Answers 3

1

Provided Api response contains 3 nested Arrays. So you have to consider them while accessing their values. Depending on the number of items you have in the array below solution will vary. In your example, You are accessing values of the first item of array 'Events', 'offers', 'outcomes'.

In this specific case, You may try

<td>{{data.events[0].offers[0].outcomes[0].line}} </td>

Sign up to request clarification or add additional context in comments.

5 Comments

It worked, can you explain why did you take 0 index?
Because its an array. How else would it know to take the first element?
Would'nt offers[0] would be pointing to first index which is id?
@AshishShah Array starts with index 0. So when you want to access first item of the Array, you need to point to Index 0. For 2nd item in the array, index 1. For 3rd item in the array , index 2 and so forth. Remember, When dealing with Arrays its always use index length-1. For Example, Array of 5 items will have max index of 4 which is length-1.
@AshishShah No. offers[0].id will return id.
0

It looks like you're not accessing the data in the offers Array.

Please, try the following code to access the events, then the offers and then the outcomes:

data.events[0].offers[0].outcomes[0].line

Hope it works!

1 Comment

You're right. I have updated the answer. Thank you!
0

Considering you have just one item in each nested aray, you can access line as

let data = {
    "events": [{
        "id": 153502679,
        "name": "Green Bay Packers @ Chicago Bears",
        "homeTeamName": "CHI Bears",
        "awayTeamName": "GB Packers",
        "startDate": "2019-09-06T00:20:00.0000000Z",
        "offers": [{
            "id": "1-2160521227",
            "label": "Point Spread",
            "outcomes": [{
                "id": "1-2568051855",
                "label": "GB Packers",
                "line": "+3.5000",
                "oddsAmerican": "-110",
                "oddsDecimal": 1.9100,
                "oddsFractional": "10/11",
                "participant": "GB Packers"
                
            }]
        }]
    }]
}

console.log(data.events[0].offers[0].outcomes[0].line);

If there are n number of objects in each nested array, you can do as

let data = {
    "events": [{
        "id": 153502679,
        "name": "Green Bay Packers @ Chicago Bears",
        "homeTeamName": "CHI Bears",
        "awayTeamName": "GB Packers",
        "startDate": "2019-09-06T00:20:00.0000000Z",
        "offers": [{
            "id": "1-2160521227",
            "label": "Point Spread",
            "outcomes": [{
                "id": "1-2568051855",
                "label": "GB Packers",
                "line": "+3.5000",
                "oddsAmerican": "-110",
                "oddsDecimal": 1.9100,
                "oddsFractional": "10/11",
                "participant": "GB Packers"
                
            }]
        }]
    }]
}


data['events'].forEach(event => {
    event.offers.forEach(offer => {
        offer.outcomes.forEach(outcome => {
          console.log(outcome.line);
        });
    });
});

2 Comments

Why 0 as index? Would'nt offers[0] would be pointing to first index which is id?
@AshishShah offers is an array of object where offers[0] would gove you the whole object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.