Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9147167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:55:20+00:00 2026-06-17T10:55:20+00:00

Hi Javascript/underscore gurus.. Lets say I receive a json object from the server which

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:55:21+00:00Added an answer on June 17, 2026 at 10:55 am

    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);});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replicate CI's humanize() and underscore() function in Javascript. From the CI
I am trying to filter through this javascript object using underscore.js, but I don't
I'm having trouble getting nested JavaScript inside Underscore templates to run. <script type=text/template id=myTemplate>
Writing a third-party javascript code which uses underscore.js and tries to avoid conflicts with
I have JavaScript application, where I use client-side templates (underscore.js, Backbone.js). Data for initial
I started reading about underscore.js today, it is a library for javascript that adds
<head> <title></title> <script src=javascript/vendor/jquery-1.6.4.min.js type=text/javascript></script> <script src=javascript/vendor/underscore.js type=text/javascript></script> <script src=javascript/vendor/backbone.js type=text/javascript></script> </head> <body> <script
JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
I'm working through some tutorial code and like most tutorial code, the json object
Given a nested object like this: var cars = { bentley: { suppliers: [

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.