1

I am not lying I am a beginner. So here is my issue. I am receiving a json object in an ajax request and would like to loop its content. I need to create the loop and gather data within the variable "pins."

function(data) {

                   var pins = [
                                   {
                                        "lat":      data.lat,
                                        "lon":      data.lon,
                                        "imageURL": data.imageURL,
                                        "title":    data.title,
                                        "subTitle": data.subTitle,
                                        "pinColor": data.pinColor,
                                        "selected": data.selected,
                                        "pinImage": data.pinImage,
                                        "index":    data.index
                                    }
                               ];
         }

Data is an object and I would like to loop every single element using jQuery.

Thanks

6
  • I am data from a json object in an Ajax request. ... success: function(data) {} etc. Commented May 27, 2013 at 19:41
  • Are you looking to loop over data to generate pins or looking to loop over pins? Commented May 27, 2013 at 19:42
  • If so, is data an array of objects that each have these attributes? Commented May 27, 2013 at 19:45
  • Yes data is the json object with all the pins. And I need to loop within my object. I need the brakets otherwise it doesn't work. Commented May 27, 2013 at 19:53
  • What are you talking about? { and } means it contains an object and [ and ] means it contains an array. Hence pins is an array of objects. Commented May 27, 2013 at 20:17

3 Answers 3

1

This is a javascript array with an object inside, and has nothing to do with jQuery (nor should you use jQuery to loop through it).

for (var i = 0; i < pins.length; i++) {
    console.log(pins[i].lat);
    console.log(pins[i].lon);
    console.log(pins[i].imageURL);
    //etc
}
Sign up to request clarification or add additional context in comments.

3 Comments

He can use jQuery, if he wants.
jQuery provides convenience methods to loop through objects, so there's no reason not to use them aside from the performance impact.
Your answer does not work. I would like a full answer where the brakets are kept since I need them to create a pin. Something like var pins = [ $.each etc...
1
$.each({ a: 1, b: 2}, function(key, value){
    console.log(key, value);
});

Comments

0

The following should work to iterate over data to collect the desired attributes from each object in the array and construct pins.

function(data) {
    var pins = [];

    for (var i=0; i < data.length; i++) {
        var new_pin = {
            "lat":      data[i].lat,
            "lon":      data[i].lon,
            "imageURL": data[i].imageURL,
            "title":    data[i].title,
            "subTitle": data[i].subTitle,
            "pinColor": data[i].pinColor,
            "selected": data[i].selected,
            "pinImage": data[i].pinImage,
            "index":    data[i].index
        };

        pins.push(new_pin);
    }
}

3 Comments

If this is the case, what exactly would be the difference between this and var pins = data;?
There would only be a difference if the OP doesn't want to collect all of the attributes... I don't exactly know what the OP is desiring here. Perhaps data isn't structured quite this simply.
If that is the case, then it may very well be functionally equivalent to setting var pins = data as h2ooooooo said. @user2415277, if you believe this answer is the solution, please accept it as such. Thank you.

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.