1

I am getting a strange behavior from the following code. The following code is from ajax success, And console.log(positions); inside loop works fine, But outside it gives me nothing.

success: function (data) {
    var positions = [];

    $.each(data, function (index, value) {
        $.each(value, function (index1, value1) {
            positions.push({
                lat: value1.rider_location.lat,
                lng: value1.rider_location.lng,
            });
            //This works fine | I can get the results. But after commenting out the below line then the last `console.log(positions)` doesn't shows anything.
             console.log(positions);
        });
    });
    console.log(positions);
}

The data from the Ajax gives me the following results then i loop through it and assign the values to positions.

enter image description here

Indside the loop gives me the following results :

enter image description here

And outside of loop console.log(positions) gives no exception as well as no results.

18
  • 1
    It gives no exceptions because you declared an empty positions, so that is what it will print when you log it. Big chance that either your data or value is empty. Try logging those inside the loop. Commented Sep 17, 2017 at 10:23
  • Seems like either data or value is an empty collection. Can you show the code where those are populated? Commented Sep 17, 2017 at 10:24
  • 1
    Exactly where are you trying to use console.log(positions)? Inside success: (as in the question) or somewhere else? (ie outside success:function(data) { ) ? Commented Sep 17, 2017 at 10:39
  • 1
    Thanks for updating the question. Can you recreate the issue within a code snippet in the question? There's nothing wrong with the code as you've provided it. I suspect you have already, but have a read of minimal reproducible example. Commented Sep 17, 2017 at 10:55
  • 1
    I can only guess that you're trying to access positions outside of the success: callback (likely even before your ajax call has started), but that's not what you've got in the code in your question, which has been shown to work correctly by two independent fiddles. Commented Sep 17, 2017 at 11:07

2 Answers 2

2

Your data is not an array but an object. Using $.each() on it means that it will loop over all of it's properties, which is why it does not work as you expect it. You probably want to loop over data.riders. Also there is no other array to loop over, so the second loop should not be there:

var data = {
    riders: [{
            rider_location: {
                lat: 2,
                lng: 3
            }
        },
        {
            rider_location: {
                lat: 4,
                lng: 5
            }
        }
    ]
};

var positions = [];

$.each(data.riders, function(index, value) {
    positions.push({
        lat: value.rider_location.lat,
        lng: value.rider_location.lng,
    });
});

console.log(positions);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

0

Given the format of the data returned I would try:

It seems as if you're trying to loop through rider_location with the second loop, but rider_location is an object, so you can just access its properties.

success: function (data) {
    var positions = [];

    $.each(data.riders, function (index, value) {
           positions.push({
                lat: value.rider_location.lat,
                lng: value.rider_location.lng,
            });
    });
    console.log(positions);
}

PS feel free to rep me :)

2 Comments

Uncaught TypeError: Cannot read property 'lat' of undefined
try console.log(value) and console.log(value.rider_location), see if youre getting the correct results.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.