Display categories of each post via JSON JS

0

I'm using blogger, and I need to "generate" the categories for each post.

function posts(json) {
 for (var i = 0; i < 11; i++) {
    var post = json.feed.entry[i];
    var postLabels = [];
  }
  for (var b = 0; b < post.category.length; b++) {
     postLabels.push(post.category[b].term);
   }
    document.write('<span>'+postLabels.join(', ')+'</span>');
  }
}

It works, but shows only the first category of each post. I need you to display all of them, how do I?

  "published": {
            "$t": "2014-09-06T22:43:00.001-07:00"
        }
        ,
        "updated": {
            "$t": "2018-04-15T13:24:56.217-07:00"
        }
        ,
        "category":[ {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Car"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Gallery"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "People"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Technology"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste"
        }
        ],
        "title": {
            "type": "text", "$t": "Photodune Vintage Car"
 }
    
asked by anonymous 16.04.2018 / 22:42

1 answer

0

This is only displaying a category, because within the first loop for you declare the variable postLabels being an array, then in the second loop for , you define it again and define its value as a string, the correct one is to add the value in postLabels .

You should change the line:

var postLabels = post.category[b].term;

To:

postLabels.push(post.category[b].term);

And change also:

document.write('<span>'+postLabels+'</span>');

To:

document.write('<span>'+postLabels.join(', ')+'</span>');

Example running:

let json = {
  "feed": {
    "entry": [
      {
        "published": { "$t": "2014-09-06T22:43:00.001-07:00" },
        "category": [
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Car" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Gallery" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "People" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Technology" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste" }
        ],
        "title": {"type": "text", "$t": "Teste A" }
      },
      {
        "published": { "$t": "2014-09-06T22:43:00.001-07:00" },
        "category": [
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Car" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste" }
        ],
        "title": {"type": "text", "$t": "Teste B" }
      },
      {
        "published": { "$t": "2014-09-06T22:43:00.001-07:00" },
        "category": [
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste A" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste B" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste C" }
        ],
        "title": {"type": "text", "$t": "Teste C" }
      }
    ]
  }
};
function posts(json) {
  for (var i = 0; i < json.feed.entry.length; i++) {
    var post = json.feed.entry[i];
    var postTitle = post.title.$t;
    var postLabels = [];

    for (var b = 0; b < post.category.length; b++) {
      postLabels.push(post.category[b].term);
    }

    document.write('<article class="item">'); 
    document.write('<h2>'+postTitle+'</h2>');
    document.write('<span>'+postLabels.join(', ')+'</span>');
    document.write('</article>');
  }
}
posts(json)

The problem of repeating the tags.

Note your code the lines I commented:

for (var k = 0; k < post.link.length; k++) {
  if (post.link[k].rel == 'alternate') {var postUrl = post.link[k].href;}
  if (post.link[k].rel == 'enclosure') {
    // post miniatura ↓
    if(post.link[k].type == 'poster imagem'){var postImage = post.link[k].href;}
    if(post.link[k].type == 'miniatura video'){var postImage = post.link[k].href;}
    if(post.link[k].href == 'http://subtitulo.com'){
      var enclosureLinkSubtitulo = post.link[k].type;
    }
    if(post.link[k].type == 'tempo real'){
      var enclosureLinkSubtitulo = "<span class='postSubTitulo' style='color:#FFF;font-weight:600;background:#cd0000;padding: 4px 5px 3px;margin-bottom:10px;display:inline-block'>TEMPO REAL</span>"
      }     
    // types icons ↓
    if(post.link[k].type == 'icone video'){var enclosureIcon = "<i class='fa fa-play'></i>";}
  }
  for (var u = 0; u < post.author.length; u++) {
    var postAuthor = post.author[u].name.$t;
  }

  // O PROBLEMA ESTA AQUI, ESTE FOR TEM QUE ESTAR FORA
  // DO FOR ATUAL.
  for (var b = 0; b < post.category.length; b++) {
      postLabels.push(post.category[b].term);
  }
}
    
17.04.2018 / 00:29