0

Myself have a javascript array,

var array = [{"div0" : [2, 1, 3, 5]}, {"div1" : [1,5,7,8]}, {"div2" : [6,9,11]}];

which contains json objects as values. I can select each object individually as array[0].div0, array[1].div1, array[2].div2.

But how can I select each object dynamically using jquery .each() function so that I can use each objects for different events something like as given in this example fiddle

3
  • 1
    That's not JSON, that's Javascript object literals. JSON is a text format to represent object. If your array was var array = ['{"div0" : [2, 1, 3, 5]}', '{"div1" : [1,5,7,8]}', '{"div2" : [6,9,11]}']; then it woult be JSON. Commented Nov 2, 2014 at 16:48
  • Thank you @Guffa for your concern. Actually the json is dynamically returning from an ajax call. So, it should be a valid json only. Here I simply given a sample to understand. Commented Nov 6, 2014 at 9:19
  • 1
    In that case it would be the entire array that is delivered as JSON and then parsed into an array of objects. The array would never contain JSON. Commented Nov 6, 2014 at 14:14

1 Answer 1

2

You could use the following syntax:

var id = $(this).prop("id");
var arr = array[index][id];
Sign up to request clarification or add additional context in comments.

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.