views:

83

answers:

4
{

    "fulltime": [

        {"name": "oscar godson", "age": "20", "email": "[email protected]"},

        {"name": "daniel erickson", "age": "25", "email": "[email protected]"},

        {"name": "john doe", "age": "18", "email": "[email protected]"}

    ],

    "parttime":[

        {"name": "bill johnson", "age": "35", "email": "[email protected]"}

    ]



}

and not knowing any of these values, e.g. fulltime could equal any thing. im looking for a function/method to loop through it all... Please, no jQuery.

Also, i want to basically get the output of: fulltime -> all inside of fulltime, parttime -> all inside of parttime, etc

A: 
for (key in your_object) {
    console.log(key + " people:");
    // "key" is "fulltime", "parttime", etc
    for (var i = 0; i < your_object[key].length; i++) {
        console.log(your_object[key][i]);
    }
}
Victor Stanciu
But, i want a more verbose output... e.g. fulltime then all the stuff inside of fulltime, then parttime, then all of whats in parttime, etc.
Oscar Godson
what about now?
Victor Stanciu
im getting fulltime people: -> fulltime, fulltime, etc, its not showing the people's data?
Oscar Godson
i updated the code, it should work now
Victor Stanciu
@Oscar Godson: I suggest you read more about the different kinds of loop statements: https://developer.mozilla.org/en/JavaScript/Guide/Statements You now know how to iterate over the properties of an object, adjusting it to your needs should be trivial.
Felix Kling
A: 

Supposing you have Firebug installed:

for(var key in json) {
  //"fulltime", "parttime"
  console.log("Checking " + key);
  for(var i = 0; i < json[key].length; i++){
    var person = json[key][i];
    //Each person
    for(var prop in person) {
      console.log(prop + ": " + person[prop]);
    }
  }
}

Edit: Be careful that you don't iterate with for ... in ... over an array. To iterate over an array, use the "regular" way with for(var i = 0; i < array.length; i++){...}

moxn
A: 

You can do that with a recursive function. But you need to be careful about circular references. See example below:

  var arr = [];

  /**
   * Gets the string representation of the specified object. This method is
   * used for debugging
   * @param {Object} Object to convert to string
   * @return {String} The string representation of the object
   */
  var toObjectSource = function(obj)   {
     if(obj === null)   {
        return "[null]";
     }
     if(obj === undefined) {
        return "[undefined]";
     }

     var str = "[";
     var member = null;
     for(var each in obj)   {
        try   {
           member = obj[each];
           if(arr.indexOf(member) === -1) { // the indexOf function is not available
                                            // on older versions of js
              arr.push(member);
              str += each + "=" + toObjectSource(member) + ", "; // but beware of this
                                                                // recursive call!!!
           }
        }catch(err) {
           alert(err);
        }
     }
     return str + "]";
  }

The reason for the check is that. It will give you "too much recursion" in case the object is like this:

 var obj = {
    "a": "a",
    "b": "b"
 }
 obj.c = obj;
naikus
A: 

First of all you can verify your JSON data in http://www.jsonlint.com/.

If you not yet convert the string with JSON to the object you should use JSON.parse function from the web browser or from http://www.json.org/js.html to convert input string to the object.

For looping through the properties of a object you can use "for in" loop. It is generally always recommended to use this loop in the following form:

for (var name in myObject) {
    if (myObject.hasOwnProperty(name)) {
        // ....
    }
}

(see for example http://www.jslint.com/lint.html#forin for the explanation). You should don't forget to declare name as var name either inside of for statement or somewhere before. If you forget this the variable will be interpret as a global and your code will run slowly.

Loop through element of an array in more effective with a standard for loop instead of "for in" loop. Moreover to receive more performance advantages you should always cache an index of a property used more as one time in a local variable. For example the the loop

for (var i = 0; i < your_object[key].length; i++) {
    console.log(your_object[key][i]);
}

should be better rewritten as following:

var arr = your_object[key];
var len = arr.length;
for (var i = 0; i < len; i++) {
    console.log(arr[i]);
}
Oleg