Skip to main content
adicionou 1323 caracteres ao conteúdo
Fonte Link
NoobSaibot
  • 9,8mil
  • 6
  • 29
  • 53

###O problema de estar repetindo as tags.

Note o seu código as linhas que comentei:

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);
  }
}

###O problema de estar repetindo as tags.

Note o seu código as linhas que comentei:

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);
  }
}
adicionou 2197 caracteres ao conteúdo
Fonte Link
NoobSaibot
  • 9,8mil
  • 6
  • 29
  • 53

###Exemplo funcionando:

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)

###Exemplo funcionando:

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)

Fonte Link
NoobSaibot
  • 9,8mil
  • 6
  • 29
  • 53

Só esta a exibir apenas uma categoria, porque dentro do primeiro laço for você declara a variável postLabels sendo um array, logo em seguida no segundo laço for, você define novamente ela e define seu valor como uma string, sendo que o correto é adicionar o valor em postLabels.

Você deve alterar a linha:

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

para:

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

E altere também:

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

para:

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