1

I am looping a object using $.each function, but I'm getting undified values.

What is wrong with my code?

var mainPageCircle = {
    circles: {
        c1: {
            color: '#730000',
            text: 'Content'
        },
        c2: {
            color: '#004f74',
            text: 'Consulting'
        },
        c3: {
            color: '#146c00',
            text: 'Commerce'
        }
    },
    radious: 100,
    stroke: 10,
    strokeColor: '#fff',
    opacity: 0.7
}

$.each(mainPageCircle, function(key, value) {
    var circles = value.circles,
        radious = value.radious;
    $.each(circles, function(index, c) {
        console.log(index, c); // i am getting error; i need index should be 0,1,2 and c should be : c1,c2,c3 values
    })
})
2
  • mainCrProp != mainPageCircle - or is that just a transcription mistake? Commented Nov 6, 2012 at 8:57
  • Does circles have a value when the error occurs? Have you tried using a browser debugging tools to place a breakpoint in the each loop investigating the objects? (F12 default key in most browsers, for Firefox get Firebug), is it the outer or inner each loop causing the problem? Have you tried running it without the inner loop? Commented Nov 6, 2012 at 8:58

3 Answers 3

3

Your each is running for all items of mainPageCircle, not just the circles defined in it. This may be more what you're aiming for:

var mainPageCircle = {
    circles :{
        c1:{color:'#730000',text:'Content'},
        c2:{color:'#004f74',text:'Consulting'},
        c3:{color:'#146c00',text:'Commerce'}
    },
    radious:100,
    stroke:10,
    strokeColor:'#fff',
    opacity:0.7
    }
$.each(mainPageCircle.circles , function (key,value) {
       var circles = value,radious = mainPageCircle.radious;
       $.each(circles, function (index,c) {
            console.log(index,c);
        });
});
Sign up to request clarification or add additional context in comments.

Comments

3

Something like this?

var mainPageCircle = {
    circles :{
        c1:{color:'#730000',text:'Content'},
        c2:{color:'#004f74',text:'Consulting'},
        c3:{color:'#146c00',text:'Commerce'}
    },
    radious:100,
    stroke:10,
    strokeColor:'#fff',
    opacity:0.7
};

var i = 0;

$.each(mainPageCircle.circles, function(){
    console.log(i, this); 
    //i: current index
    //this: c1, c2 etc
    //use properties on this to fetch the values
    //this.color for example
    i++;
});​

You won't be able to use key in your example as an index integer, since it will fetch the object key, and not the current index in the loop.

Fiddle

4 Comments

.each already provides the index and the element value in the callback. As per docs callback(indexInArray, valueOfElement). There should be no need to track your own with i/i++. You should be able to do: $.each(mainPageCircle.circles, function(index){ console.log(index, this) }
@FrançoisWahl Check out this fiddle: jsfiddle.net/PxHFg/1 . As i said in the post, indexInArray will be the object key in this case.
Ah,..I see. I only see now in documentation further down the comments regarding the use of .each on a collection of objects where index becomes the key. When I read your post I didn't expect that at all, kind if didn't make sense to me at the time of reading. Nice one. :) +1 for that and sorry for the mix-up.
@FrançoisWahl Hehe no problem, its pretty confusing until you've read the entire API page about the loop ;-)
2

I’m not sure what your intention is, but you probably meant to do something like:

var mainPageCircle = {
    circles :{
        c1:{color:'#730000',text:'Content'},
        c2:{color:'#004f74',text:'Consulting'},
        c3:{color:'#146c00',text:'Commerce'}
    },
    radious:100,
    stroke:10,
    strokeColor:'#fff',
    opacity:0.7
}

var circles = mainPageCircle.circles,
    radious = mainPageCircle.radious;
$.each(circles, function (index, c) {
    console.log(index,c); // c1, props, etc...
    console.log(parseInt(index,10)); // 1,2,3...
});

Comments

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.