-6

i write javascript code to parse json and use the data, the json file contain date and time, latitude and longitude data :

const api_url = 'json.php'
        async function getjson() {
            const response = await fetch(api_url);
            const data = await response.json();
            console.log(data)

the console log show 260 array, but the latest date and time is on potition array : 260 my question is how i can get the latest json data sort in date and time?

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Justinas Oct 12 '21 at 20:54
  • no sir im sorry, can you give me example with my code? – Redynan frozt Oct 12 '21 at 21:02
  • For future reference, only add tags that are relevant to the question. This has nothing to do with either PHP or HTML. – M. Eriksson Oct 12 '21 at 21:19
  • Does this answer your question? [Get the last item in an array](https://stackoverflow.com/questions/3216013/get-the-last-item-in-an-array) – jsonderulo Oct 12 '21 at 21:24

1 Answers1

0

If you know the array is sorted and want to get the last item in the array, you can use the length property to calculate the index of the last item e.g.

const latest = data[data.length -1];
Mohammed Farag
  • 335
  • 2
  • 9
  • amazing, your code works like charm. thankyou so much for your help Mr. mohammed :-) – Redynan frozt Oct 12 '21 at 21:09
  • 1
    @Redynanfrozt Thank you! I understand you're new to coding and to StackOverflow, welcome aboard! Please take a moment to remove the php and html tags from the post since your question is purely js. Keep learning and keep improving your google searches ;) – Mohammed Farag Oct 12 '21 at 21:12
  • thankyou so much for warm welcome and the spirit Mr. mohammed, i will follow your advice hope become master javascript like you are :-) – Redynan frozt Oct 13 '21 at 05:18