4

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?

3
  • 2
    Your code is invalid. You can't write var foo.bar = ...;. Get rid of the var and see if that helps. Commented Jan 23, 2013 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. Commented Jan 23, 2013 at 9:03
  • pardon. Skip the var, the parameter doc._id is passed into the function. Commented Jan 23, 2013 at 9:07

3 Answers 3

2

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.

0
1

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 
    }
}
0

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.