0

Good evening,
I'm facing a simple problem in JSON. I'm developping a smartwatch (Pebble) app which gets data from an API, which returns the following :

{
  "name": "toto",
  "count": 55,
  "results": {
    "collection1": [
      {
        "article": "Content number 1"
      },
      {
        "article": "Content number 2"
      },
      {
        "article": "Content number 3"
      }
    ]
  }
}

The problem is the following : is it possible to access only the latest "article" content from this JSON ? So in this case, it would be "Content number 3".
Here is what I've got so far, but it returns only the 1st element :

ajax({ url: 'MY_API_URL', type:'json' }, function(data){
  simply.vibe(); // Vibrate the watch
  var message = data.results.collection1[0].article;
simply.body(message); // display the message on the watch


Thanks a lot !

Frankynov
  • 65
  • 2
  • 8
  • and [Selecting Last Element in JavaScript Array](http://stackoverflow.com/q/9050345/218196), both found via [Google](https://www.google.com/search?q=javascript%20array%20get%20last%20element). – Felix Kling Jul 11 '14 at 19:33

2 Answers2

3

That would be:

data.results.collection1[data.results.collection1.length - 1].article
njzk2
  • 38,969
  • 7
  • 69
  • 107
1

data.results.collection1[data.results.collection1.length-1].article;

smistry
  • 1,127
  • 1
  • 9
  • 9