0

i have a basic array of object coming from an api something like:

[
{date:'2020-10-02, number: 400}, 
{date:'2020-10-01, number: 200},
... ]

And so on, actually i don't know how many items are in the array, but i need to get the last two

i corerctly download and store the data

my

console.log(data.lenght);

Shows the correct lenght of array (222 at the moment of writing);

As i said i need to show the latest date, the actual number and the difference with previous date; if i manually set the numbers it works as expected:

 <Text>
        {data[222].date} - {data[222].number}
        Variation {data[222].number - data[221].number}
 </Text>

but if i try to use the length of data as the index it give me an error: undefined is not an object (evaluating 'data[len].date'

len=data.length
...
 <Text>
    {len}
    {data[len].date} - {data[len].number}
    Variation {data[len].number - data[len-1].number}
 </Text>

len is printed correctly, i also tried to place it in {} double {{}} and other things like that i was wondering how can i access object using a "calculated" index

1 Answer 1

1

data.length will show total items, but array starts from 0,

{data[len - 1].date} - {data[len - 2].number}

should solve

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

Comments

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.