Possible Duplicate:
Find object by id in array of javascript objects
So I have this:
mapArray=
[{"Title":"crop_rotation","subs":"5,6,7,10"},
{"Title":"cover_crops","subs":"2,5,7,8,9,13,14"},
{"Title":"binary_wetlands","subs":"1,2,3,4,5,6,7,8,9,10,11"}]
I am trying to access the subs value based on the Title. I am trying
listofSubs=mapArray["crop_rotation"]("subs");
I don’t get anything returned. What am I missing here? I need to take that list and convert to a string but I assume it will come out as a string since I have the object already parsed? Thanks in advance
First you have to find the object with the specific title. You have to do this by iterating over the array and comparing each object’s
Titleagainst your value:Then you can access the corresponding
subsproperty:An explanation for why your code does not work:
|1|tries to access the propertycrop_rotationof the object inmapArray. Arrays don’t have such properties and in your case no object does.crop_rotationis the value of one of the properties.|2|is a function call. It tries to call the function referenced bymapArray["crop_rotation"]and passes"subs"as first argument. It throws an error ifmapArray["crop_rotation"]is not a function (like in your case).Further information: