最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - underscore javascript _.each loop for properties nested array - Stack Overflow

programmeradmin1浏览0评论

Hi Javascript/underscore gurus..

Lets say I receive a json object from the server which has an anonymous array nested as one of its properties... how would i loop through that array in an underscore _.each method?

This is my json object:

  "onlineUsers": [
    {
      "Id": "users/2",
      "Name": "Hamish",
      "LatestActivity": "2013-01-17T04:02:14.2113433Z",
      "LatestHeartbeat": "2013-01-17T04:02:14.2113433Z"
    },
    {
      "Id": "users/3",
      "Name": "Ken",
      "LatestActivity": "2013-01-17T03:45:20.066Z",
      "LatestHeartbeat": "2013-01-17T04:04:34.711Z"
    }
  ]

how would I modify this function to print out the names?

_.each(onlineUsers, function(user){log(user.name);});

This is printing out the actual collection of nested objects, obviously because they are elements in the nested array of onlineUsers... not sure how to get to that array to loop if it is anonymously passed in...

Thanks, Hamish.

Hi Javascript/underscore gurus..

Lets say I receive a json object from the server which has an anonymous array nested as one of its properties... how would i loop through that array in an underscore _.each method?

This is my json object:

  "onlineUsers": [
    {
      "Id": "users/2",
      "Name": "Hamish",
      "LatestActivity": "2013-01-17T04:02:14.2113433Z",
      "LatestHeartbeat": "2013-01-17T04:02:14.2113433Z"
    },
    {
      "Id": "users/3",
      "Name": "Ken",
      "LatestActivity": "2013-01-17T03:45:20.066Z",
      "LatestHeartbeat": "2013-01-17T04:04:34.711Z"
    }
  ]

how would I modify this function to print out the names?

_.each(onlineUsers, function(user){log(user.name);});

This is printing out the actual collection of nested objects, obviously because they are elements in the nested array of onlineUsers... not sure how to get to that array to loop if it is anonymously passed in...

Thanks, Hamish.

Share Improve this question edited Sep 12, 2013 at 20:16 Thaddeus Albers 4,1925 gold badges36 silver badges43 bronze badges asked Jan 17, 2013 at 3:53 HCdevHCdev 5501 gold badge6 silver badges20 bronze badges 2
  • removing the outer {} should make it valid JSON. – Joseph Commented Jan 17, 2013 at 3:56
  • At a minimum, the inner objects should be {name: 'Joe'},{name: 'bloggs'}. That alone makes it an invalid object and throws a JS error. – Dennis Rongo Commented Jan 17, 2013 at 4:10
Add a comment  | 

2 Answers 2

Reset to default 14

The JSON you are receiving from the server is invalid JSON. The array needs a property name, eg:

onlineUsers = { names: [{name : "Joe"}, {name : "bloggs"}]}

Then you could do this:

_.each(onlineUsers.names, function(user){log(user.name);});

An anonymous array inside an object is not valid json, so you wouldn't be able to parse it.

either give the array a name or remove the outer object.

发布评论

评论列表(0)

  1. 暂无评论