Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
2  
Your code is invalid. You can't write var foo.bar = ...;. Get rid of the var and see if that helps. – Blender Jan 23 at 9:01
indices[i]["indexAB"] and indices[i][index_name] should always yield the same outcome, given that the value of index_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:03
pardon. Skip the var, the parameter doc._id is passed into the function. – frequent Jan 23 at 9:07

3 Answers

up vote 1 down vote accepted

This is not a direct answer to your question but I believe that it may actually help you more than giving you a complicated way to access values in your object.

If instead of this JSON object:

var index_file =
[{
 "indexAB":[
    { "postsome": ["keyword_abc", "keyword_def"] },
    { "testsome": ["keyword_111", "keyword_222"] }
  ]
},{
 "index_random": [
    { "postsome": ["keyword_abc"] }
  ]
}];

you would have this much simpler data structure:

var index_file =
{
  "indexAB": {
    "postsome": ["keyword_abc", "keyword_def"],
    "testsome": ["keyword_111", "keyword_222"]
  },
  "index_random": {
    "postsome": ["keyword_abc"]
  }
};

then it would be much easier to access, using just:

var value = index_file.indexAB.postsome[0]; // no loops, no nothing
//  value == "keyword_abc"

See: DEMO

I think that what you should change is your data model because currently it is something that is very far from the idea of JSON and it will always be very hard do access data in it.

share|improve this answer
good idea. I think I will give that a try. Thanks! – frequent Jan 23 at 9:08

A couple of issues

  • "indexAB" only exists on the first element in the array
  • you cannot have dots inside variable names.

I suggest you test whether indexAB is a property of the object before deferencing it further. See example below:

Fixed

var indices = index_file;
for (var i = 0, l = indices.length; i < l; i++) {

    var doc_id = "postsome";
    var index_name = "indexAB";

    indices[i]["indexAB"];             // ok, returns object on correct iteration
    indices[i][index_name];            // undefined
    if ("indexAB" in indices[i]) {
      indices[i].indexAB[0][doc_id]     // ok, returns undefined or keywords
      indices[i][index_name][0][doc_id] // undefined 
    }
}
share|improve this answer

index_name is undefined because the line prior to that raises an error

doc._id = "postname" // this causes an error

Just use a simple string

doc = "postname"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.