0
 var Mydata =
        {
            "prop1":
                    [
                        { "a": "parties", "b": "0.006" , "c": "0.9"},
                        { "a": "royal-challenge",  "b": "0.02" , "c": "0.333" },
                        { "a": "star-speak","b": "0.02","c":"0.1" }
                    ],
            "prop2":
                    [
                        { "a": "parties","b": "0.006" , "c": "0.9"},
                        { "a": "star-speak","b": "0.02", "c": "0.009" }
                    ],
            "prop3":
                    [
                        { "a": "parties","b": "0.006" , "c": "0.9"}
                    ]
        };

My goal is to return values of a,b,c like the following--->

parties,0.006,0.9
royal-challenge,0.02,0.333 and so on for all the properties.

I am facing problems in iteration.

I am doing something like this---:

for(var i in Mydata){
           return i[a],i[b],i[c]
        }
3
  • wait for 5 min..updating Commented Jul 5, 2013 at 9:58
  • 2
    for .. in cycle will assing KEY to your i variable, not value; to access to property, you need to use Mydata[i] in cycle body. Commented Jul 5, 2013 at 10:13
  • @Tommi:yes,understood from the answers!!!thank you Commented Jul 5, 2013 at 10:34

4 Answers 4

1

You could use a for..in loop to go through the keys, check for an array and then loop the array items. Something like this?

var newArray = []; // store the results in a new array

for (key in Mydata) { // loop prop1, prop2 etc
    if (Mydata[key].forEach) { // check for array
        Mydata[key].forEach(function(item) { // loop array
            newArray.push(item.a + ", " + item.b + ", " + item.c);
        });
    }
}

http://jsfiddle.net/CR2vY/

Note: The forEach method is not available in older browsers (like ie8), but is easily shimmed. More information at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Sign up to request clarification or add additional context in comments.

2 Comments

1)Mydata[key].forEach---->how is this checking for array,2)function(item)--->where is this item parameter coming from
@GENES the forEach method will only be available on arrays (undefined = falsy, meaning skip if there is no forEach method). Read the MDN link i posted to read more about it (including the item parameter).
0
for(var head in Mydata) {


    for(var leave in Mydata[head]) {
        document.write(Mydata[head][leave]['a']);
        document.write(',');
        document.write(Mydata[head][leave]['b']);
        document.write(',');
        document.write(Mydata[head][leave]['c']);
    }
}

Comments

0

Try this.

for(var index in Mydata){
   var propArray = Mydata[index];
   for(var i=0; i<propArray.length; i++){
      var prop = propArray[i];
      var opString = '';
      for(var propIndex in prop){
          opString +=prop[propIndex]+',';
      }
      console.log(opString.substr(0, opString.length-1))
   }

}

Comments

0

Try this (it should work in all the browsers), result array will contain all the objects with which you can iterate over

var result = [];
for (var key in Mydata) {
   if (Mydata.hasOwnProperty(key)) {
    for (var key2 in Mydata[key]) {
        if (Mydata[key].hasOwnProperty(key2)) {
          console.log(key2, Mydata[key][key2]);
          result.push(Mydata[key][key2]);
        }
    }  
   }
}
result;

Comments

Your Answer

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