1

In the view, I want to loop through list and render resolved_title of all list items. This is the JSON object.

{ 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 }

And below is the view, but I'm getting body.list.forEach is not a function error. What am I doing wrong?

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

1 Answer 1

1

list is not Array is Object and in Object there is not method forEach, try this

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

But better format data in your controller and then pass it to view, like so

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>
<% }) %>
Sign up to request clarification or add additional context in comments.

1 Comment

It works, thanks! I didn't know about this. Please give me a few minutes to accept your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.