2

I am working with jquery ajax & the ajax response is multidimensional json array, I am assinging the JSON values to coordinates array & then assinging coordinates array to new JSON coord_set, after assinging all values to coord_set, It takes last array values to all,

for e.g. the JSON result contains following values

obj[0]={125, 343, 456, 453},
obj[1]={345, 657, 234, 787},
obj[2]={980, 677, 657, 568}

after assinging to new JSON the values are:

coord_set[0] = {
                    fillColor : 'rgba(255, 234, 111 ,0.5)', 
                    data : [980, 677, 657, 568]
               }
coord_set[1] = {
                    fillColor : 'rgba(255, 234, 111 ,0.5)', 
                    data : [980, 677, 657, 568]
               }
coord_set[2] = {
                    fillColor : 'rgba(255, 234, 111 ,0.5)', 
                    data : [980, 677, 657, 568]
               } 

This is my code:

  var obj = JSON.parse(data);
  for(var j=0;j<obj.length;j++)
  { 
      for (var i=0;i<obj[j].length;i++)
      {
                  coordinates[i] = obj[j][i].how_many;
      }

      coord_set[j] = { fillColor : 'rgba(255, 234, 111 ,0.5)', data : coordinates };
  }
  alert(JSON.stringify(coord_set));

Please tell me, If I am doing Anything wrong in my code?

2
  • Are you getting an error? You didn't mention what problem you were having. Commented Aug 19, 2013 at 5:38
  • Not getting any error, the problem is last array of ie. obj[2] values are getting assigned to all coord_set JSON array Commented Aug 19, 2013 at 5:42

2 Answers 2

2

The problem is that you're using a single coordinates array. You keep setting and re-setting the values in that same array, and you keep storing that same array in new elements of coord_set. To fix this, you just need to use a new coordinates array on each pass through the outer loop:

  for(var j=0;j<obj.length;j++)
  { 
      coordinates = [];      // <----- add this
      for (var i=0;i<obj[j].length;i++)
Sign up to request clarification or add additional context in comments.

2 Comments

don't forget to make it a local variable, otherwise, you may have other surprises later...
Thanks a lot ruakh!!! Your fix made my day...... was stuck in this for the entire day today.....
0

I'm gonna assume that JSON.parse actually works... but since you said you are using jquery, i would use http://api.jquery.com/jQuery.parseJSON/ personally...

Few things about your code:

  • you never create a new object for coordinates, which is, by default, a global variable in JS. You must type:

    var coordinates = []; // before the for (var i = 0; ...
    
  • where is this property "how_many" coming from? I don't see it in your first block of code... The code in your i loop should be:

    coordinates[i] = obj[j][i];
    
  • finally, why do you need a copy of this transient object anyway? That should just do it:

    coord_set[j] = { fillColor : 'rgba(255, 234, 111 ,0.5)', data : obj[j] };
    // if you change your result set to give a code like that:
    obj[0]= [ 125, 343, 456, 453 ] // and not: obj[0]={125, 343, 456, 453}
    

1 Comment

From the OP's posted output, I'm assuming that the how_many stuff is actually fine. Probably the reason for copying the object is that the JSON stores the individual values in objects containing fields named how_many, whereas the OP just wants the values directly in data.

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.