I'm clueless. I have a JSON string like this which I need to check for a supplied "property" (postsome
in the following example):
var index_file =
[{
"indexAB":[
{ "postsome": ["keyword_abc", "keyword_def"] },
{ "testsome": ["keyword_111", "keyword_222"] }
]
},{
"index_random": [
{ "postsome": ["keyword_abc"] }
]
}]
There my be any number of indices ("indexAB", "index_random") with n
objects inside.
I need to "find" my property postsome
but I cannot get it to work, because I'm struggling with the correct way of accessing the object.
So:
for (var i = 0, l = indices.length; i < l; i += 1) {
doc._id = "postsome",
index_name = "indexAB";
indices[i]["indexAB"]; // ok, returns object on correct iteration
indices[i][index_name]; // undefined
indices[i].indexAB[0][doc._id] // ok, returns undefined or keywords
indices[i][index_name][0][doc._id] // undefined
}
Question:
How can I access a nested object in loop using a variable name index_name
?
var foo.bar = ...;
. Get rid of thevar
and see if that helps. – Blender Jan 23 at 9:01indices[i]["indexAB"]
andindices[i][index_name]
should always yield the same outcome, given that the value ofindex_name
really is"indexAB"
. If you really have the error Blender points out in your code, you shouldn't be any of those lines, though. – David Hedlund Jan 23 at 9:03var
, the parameterdoc._id
is passed into the function. – frequent Jan 23 at 9:07