1

I am not able to retrieve the data from my json object; the following code return undefined for var r in the console but the very similar code (shown below) works in JSFiddle.

 var currentIMGArray = new Array();

 $.ajax({
 url:"foo.php",
 type: "post",
 data: {
  action: "foo",
  parameter: foo,
 },
 dataType: "json",
 success: function (result) {
  currentIMGArray = result[3];
  console.log(currentIMGArray);
 },
 });


 $(document).ready(function(){

  $("#W").on("click", ".IMG", function() {

   console.log("currentIMGArray: "+currentIMGArray.toString());

   var IMGid = $(this).attr("id");
   console.log("IMGid: "+IMGid);

   var r = currentIMGArray.IMGid;
   console.log("r: "+r);
 })
})

Here is the console output:

[Log] {imgGallery0: "1.jpg", imgGallery1: "2.jpg"} 
[Log] currentIMGArray: [object Object]
[Log] IMGid: imgGallery0
[Log] r: undefined

But this code works in JSfiddle so Im confused where the difference is ?

var arr = {imgGallery0: "1.jpg", imgGallery1: "2.jpg"};
var b = arr.imgGallery1;
console.log(b)
3
  • can u try JSON.parse (currentIMGArray) and paste the result here Commented Jan 8, 2018 at 15:12
  • I think that you found the correct way, but, just one tip: instead console.log("string " + anyObject.toString()); you can use this: console.log("string ", anyObject); Commented Jan 8, 2018 at 15:26
  • thanks @MarceloRafael I'll try that next time Commented Jan 8, 2018 at 15:33

2 Answers 2

1

Use var r = currentIMGArray[IMGid]; instead of var r = currentIMGArray.IMGid;

Since IMGid is a variable and can change its values, you need to access the content using an array style approach currentIMGArray[IMGid] this means whichever is in the IMGid variable imgGallery0, imgGallery1 get that exact key from the currentIMGArray object. Imagine if you had something like {imgGallery0: "1.jpg", imgGallery1: "2.jpg", IMGid: '3.jpg'} how would you get the 3.jpg? currentIMGArray.IMGid in this case will return 3.jpg

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

2 Comments

@jotyhista Since IMGid is a variable and can change its values, you need to access the content using an array style approach currentIMGArray[IMGid] this means whichever is in the IMGid variable imgGallery0, imgGallery1 get that exact key from the currentIMGArray object. Imagine if you had something like {imgGallery0: "1.jpg", imgGallery1: "2.jpg", IMGid: '3.jpg'} how would you get the 3.jpg? currentIMGArray.IMGid in this case will return 3.jpg
I see. Thank you I did not know that; explains why JSfiddle worked.
0

try it like this:

var r = currentIMGArray[IMGid];

when you put var r = currentIMGArray.IMGid you are trying to access an attribute named IMGid on the currentIMGArray object, thats why it returns undefined

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.