0

My JSON text is

[{
        "album_name" : "marriage",
        "album_image" : "images/abc.jpg"
    }, {
        "album_name" : "holiday",
        "album_image" : "images/holi.jpg"
    }
]

In that i need to get index value of each object. I fetched above values using for loop and appended dynamically. My requirement is when i click on each item i need to pass that index value of that object in onclick function.

3
  • 1
    what do you mean by index value? as in you need holiday, image_url ?? Commented Dec 9, 2014 at 6:54
  • above array having 2 objects, i need object index value when loop in that section object, and i need to pass that index value in another function. Commented Dec 9, 2014 at 7:06
  • Please help me here jsfiddle.net/o77uLru7/2 Commented Dec 9, 2014 at 7:13

2 Answers 2

1

Following json object

 var json = [{"album_name":"marriage","album_image":"images/abc.jpg"},
             {"album_name":"holiday","album_image":"images/holi.jpg"}];

if you want to get any index value then you can get this way

console.log(json[0].album_name); // means 0 index album_name key value

use forEach function to iterate JSON object,

var json = [{"album_name":"marriage","album_image":"images/abc.jpg"},
            {"album_name":"holiday","album_image":"images/holi.jpg"}];

json.forEach(function(object, index){
 console.log('Index ==>' + index, ' Object data' + object);
 //or specific key value of object
 console.log(object.album_name);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Supply a description and this answer would be perfect.
@MixedVeg no need 2 loops, see this jsfiddle.net/o77uLru7/4 and you have used json for loop variable that is json object
Thanks @Girish I dnt know where my mind was that time
0

You can also try this

   $.each(json, function(i, obj) {  
      $.each(obj, function(index, value){
        console.log(index);
      });
  });

And here is jsfiddle

1 Comment

@MixedVeg: plz go through with following article metaltoad.com/blog/…

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.