0

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

Community
  • 1
  • 1
jeynon
  • 322
  • 6
  • 16

4 Answers4

3

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 Title against your value:

function find(arr, key, value) {
    for(var i = 0, l = arr.length; i < l; i++) {
        if(arr[i][key] === value)) {
            return arr[i];
        }
    }
    // return {}; // if you would want it null-safe
}

Then you can access the corresponding subs property:

var obj = find(mapArray, 'Title', 'crop_rotation');
if(obj) {
    var listofSubs = obj.subs;
}

An explanation for why your code does not work:

mapArray["crop_rotation"]("subs");
//      |-      1      -|-   2  -|
  • |1| tries to access the property crop_rotation of the object in mapArray. Arrays don't have such properties and in your case no object does. crop_rotation is the value of one of the properties.
  • |2| is a function call. It tries to call the function referenced by mapArray["crop_rotation"] and passes "subs" as first argument. It throws an error if mapArray["crop_rotation"] is not a function (like in your case).

Further information:

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Excellent answer and a piece of code I will put in the arsenal. Thanks for your help Felix. – jeynon Oct 10 '12 at 19:03
  • $.each(forMapArray, function(index, value) { if (forMapArray[index]["Title"]=="crop_rotation"){ alert("YES"); doCropRotation(); } }); – jeynon Oct 10 '12 at 19:40
  • @jeynon: Yep, for processing each object that fullfils a certain criteria, this makes sense. Note that you can also just do: `if(value["Title"]=="crop_rotation"){...}` – Felix Kling Oct 10 '12 at 20:31
0

alert(mapArray[0].Title) = "crop_rotation"

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

What you have is an array of objects there. And arrays are numerically indexed. So to get the value you are looking for:

mapArray;    // returns array of objects
mapArray[0]; // returns first object in the array

// returns the value of the `"subs"` key in the first object in the array
mapArray[0]['subs'];

Note this wont find it by title, you need a bit more logic for that. See Felix Kling's answer to do that,

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
0

You can' just access list(array) by field of it's element. It's named list(array) for purpose - it's not indexed by anything but position. So you can access list like this mapArray[0]. If want to access it by title You should have structur like this

{ "crop_rotation": ...,
  "cover_crops" :....,
  .... 
}

If your input is fixed that you can't change it you have to pre-process it to fit your needs. So you have to iterate over the array and make dictionary out of it changing "Title" values into keys of your new dictionary.

yakxxx
  • 2,841
  • 2
  • 21
  • 22