mardi 5 mai 2015

access anonymous property from object

I have a set of nesting json object like this:

var obj = {
  name: "student",
  contact: {
     phone: "22222",
     fax: "33333",
     ...
  },
  ...
}

And I have these text fields:

<input type="text" name="name" />
<input type="text" name="contact_phone" />
<input type="text" name="contact_fax" />
...

Now I want to fill these fields with appropriate property from above object. My question is how can I access anonymous property from that object? For example suppose I have this jquery code:

 $("#formID").find("input").each(function(index) {
    fieldName = $(this).attr("name");
    var namePart = fieldName.split("_");
    //I want something like this: console.log(obj.namePart[0].namePart[1])
});

Aucun commentaire:

Enregistrer un commentaire