CodingT
CodingT

Reputation: 31

How to get last elements array in blogger feeds

I'm trying to grab the data feed on my blog with the help of blogger json. I made it with the help of javascript to retrieve the data, but I was confused about displaying the last data in an array of feeds. Whereas what is shown is all the arrays based on the labels in the feeds.

My question: how to show only the last data in an array in blogger feeds.

function series(e) {
  for (var t = 0; t < e.feed.entry.length; t++) {
    var r, l = e.feed.entry[t],
      n = l.title.$t;
    if (t == e.feed.entry.length) break;
    for (var i = 0; i < l.link.length; i++)
      if ("replies" == l.link[i].rel && "text/html" == l.link[i].type && (l.link[i].title, l.link[i].href), "alternate" == l.link[i].rel) {
        r = l.link[i].href;
        break
      }
    document.write('<a href="' + r + '" title="' + n + '">' + n + "</a>")
  }
}
<script src="https://anitoki.malestea.com/feeds/posts/default/-/Horimiya?orderby=published&amp;alt=json-in-script&amp;callback=series&amp;max-results=999"></script>

Upvotes: 2

Views: 159

Answers (2)

Bouh
Bouh

Reputation: 1392

This code shows only the last entry in the array using arr.slice(-1), if you meant first element just remove .slice(-1) from the code

function series(response) {
    if (response.feed.openSearch$totalResults.$t > 0) {
        var entry = response.feed.entry.slice(-1)[0],
            title = entry.title.$t,
            url = entry.link.filter(function(e) {
                return e.rel === "alternate";
            })[0].href;

        document.write('<a href="' + url + '" title="' + title + '">' + title + "</a><br/>");

    } else {
        // no posts
    }
}

Upvotes: 1

krishna
krishna

Reputation: 1

function series(e) {

  var r, l = e.feed.entry[e.feed.entry.length - 1],
    n = l.title.$t;
  for (var i = 0; i < l.link.length; i++) {
    if ("replies" == l.link[i].rel && "text/html" == l.link[i].type && (l.link[i].title, l.link[i].href), "alternate" == l.link[i].rel) {
      r = l.link[i].href;
      break
    }
    document.write('<a href="' + r + '" title="' + n + '">' + n + "</a>")
  }

}

Upvotes: 0

Related Questions