I have a javascript object and I need to reference the value of one of it’s children. The child should be part of an array.
This works:
this.manager.response.highlighting[doc.id]['sentence_0002']
But this doesn’t:
this.manager.response.highlighting[doc.id][0]
I don’t know which sentence_000* numbers are going to be returned, so I want to reference it by it’s array number.
this.manager.response.highlighting[doc.id].length
doesn’t return anything either.
Here is a portion of the xml document that was turned into the javascript object:
<response>
<lst name="highlighting">
<lst name="http://www.lemonde.fr/international/">
<arr name="sentence_0005">
<str> puni pour sa gestion de la crise Geir Haarde a été condamné pour avoir manqué aux devoirs de sa </str>
What I need to access is the value in <str>. doc.id is successfully set to http://www.lemonde.fr/international/.
If
highlighting[doc.id]has a property with a name likesentence_xyz, there is no positional order to that property, but you can find out what keys exist using afor..inloop:You may find you need to filter out other properties, which you can do with the usual string methods, e.g.:
You may also find
hasOwnPropertyuseful, though I’m guessing this is a deserialized object graph from a JSON text response, in which casehasOwnPropertydoesn’t really come into it.