在node.js中引用带有forEach的JSON对象

时间:2015-10-12 15:24:52

标签: javascript json node.js

在视图中,我想遍历list并渲染所有列表项的resolved_title。这是JSON对象。

{ status: 1,
  complete: 1,
  list: 
   { '56777886': 
      { item_id: '56777886',
        resolved_id: '56777897',
        given_url: 'https://youtu.be/5XD2kNopsUs',
        given_title: '',
        favorite: '0',
        status: '0',
        time_added: '1444084808',
        time_updated: '1444392563',
        time_read: '0',
        time_favorited: '0',
        sort_id: 2,
        resolved_title: 'Jason Fried: Why work doesn\'t happen at work',
        resolved_url: 'https://www.youtube.com/watch?v=5XD2kNopsUs&feature=youtu.be',
        excerpt: 'http://www.ted.com Jason Fried has a radical theory of working: that the office isn\'t a good place to do it. At TEDxMidwest he lays out the main problems (call them the M&Ms) and offers three suggestions to make work work.TEDTalks is a daily video podcast of the best talks and performances from the',
        is_article: '0',
        is_index: '0',
        has_video: '2',
        has_image: '1',
        word_count: '0' },
     '240393304': 
      { item_id: '240393304',
        resolved_id: '234209466',
        given_url: 'https://youtu.be/WAuDCOl9qrk',
        given_title: '',
        favorite: '0',
        status: '0',
        time_added: '1444084841',
        time_updated: '1444392557',
        time_read: '0',
        time_favorited: '0',
        sort_id: 1,
        resolved_title: 'John Maeda: How art, technology and design inform creative leaders',
        resolved_url: 'http://www.youtube.com/watch?v=WAuDCOl9qrk&feature=youtu.be',
        excerpt: 'John Maeda, President of the Rhode Island School of Design, delivers a funny and charming talk that spans a lifetime of work in art, design and technology, concluding with a picture of creative leadership in the future. Watch for demos of Maeda\'s earliest work -- and even a computer made of people.T',
        is_article: '0',
        is_index: '0',
        has_video: '2',
        has_image: '1',
        word_count: '0' },
     '714669196': 
      { item_id: '714669196',
        resolved_id: '714669201',
        given_url: 'https://youtu.be/U8-Q70gV2Yk',
        given_title: '',
        favorite: '0',
        status: '0',
        time_added: '1444084869',
        time_updated: '1444575340',
        time_read: '0',
        time_favorited: '0',
        sort_id: 0,
        resolved_title: 'From Storytelling to Storylistening: John Maeda (Future of StoryTelling 2014)',
        resolved_url: 'http://www.youtube.com/watch?v=U8-Q70gV2Yk&feature=youtu.be',
        excerpt: 'http://futureofstorytelling.orgSee the rest of our 2014 FoST films here: https://www.youtube.com/playlist?list...Esteemed designer John Maeda discusses how successful leaders apply design thinking and start with storylistening before they get to storytelling.A Future of StoryTelling Film.Find us on',
        is_article: '0',
        is_index: '0',
        has_video: '2',
        has_image: '1',
        word_count: '0' } },
  error: null,
  search_meta: { search_type: 'normal' },
  since: 1444662727 }

以下是视图,但我收到body.list.forEach is not a function错误。我做错了什么?

<% body.list.forEach(function(item) { %>
    <p><%= item.resolved_title %></p>
<% }) %>

1 个答案:

答案 0 :(得分:1)

list Array不是ObjectObject中没有方法forEach,请尝试此操作

<% Object.keys(body.list).forEach(function(key) { %>
    <p><%= body.list[key].resolved_title %></p>
<% }) %>

但更好的格式化控制器中的数据,然后将其传递给视图,就像这样

var titles = Object.keys(body.list).map(function(key)
  return body.list[key].resolved_title;
});

// pass titles to view 

<% titles.forEach(function(title) { %>
    <p><%= title %></p>
<% }) %>