0

My goal is to be able to keep track of my position/depth in a JSON tree by adding elements to an Array and then access nested nodes in the JSON utilizing this Array. By now say the Array foo it has one element:

foo = ["customers"]

so that element would act as a reference for a JSON children, say:

jsonTree["customers"]

where jsonTree is something like:

{
"customers":{
  "name": "J. Goldsmith",
  "orders": [{
    "order": "1",
    "order": "2"
  }]
 }
}

Then foo eventually varies its size and become

foo = ["customers","orders"]

So the JSON reference would become

jsonTree["customers"]["orders"]

Now say that customers.orders can become customers.orders.order.date.etc.etc... Is there any way to build the jsonTree reference programmatically with N dimensions based on the N elements of foo Array?

Some examples:

I have ["John","Williams"] -> i want to build composer["John"]["Williams"] ["Erich","Wolfgang","Korngold"] -> i want to build composer["Erich"]["Wolfgang"]["Korngold"]

14
  • 1
    Would you also explain what you intend to do with this? It seems like a helper construct of some sort, what problem are you trying to solve? Commented Jul 10, 2013 at 15:43
  • 1
    Additionally, your sample object is not syntactically valid. Please set up a working sample on jsFiddle that demonstrates your problem. Commented Jul 10, 2013 at 15:46
  • 1
    @RocketHazmat An array of arrays has one dimension, that's the difference. Some of its members might be other one-dimensional arrays, but they don't have to be. It's a nested data structure. A multi-dimensional array is uniform. You can have an array of arrays that behaves like a multi-dimensional array, but that's neither the only way to build it nor is it enforced in any way. Commented Jul 10, 2013 at 15:52
  • 1
    @RocketHazmat, to be honest, I wasn't really sure either, but my friend Google revealed the following to me: stackoverflow.com/questions/597720/…. Although that question is specifically about C, the answers suffice for JS. Commented Jul 10, 2013 at 15:53
  • 1
    @Tomalak: So, a multi-dimensional array is an array of arrays where the dimensions are enforced? I never knew a structure like that existed! Guess that's what I get for coming from a PHP/JavaScript background. I've always used the term "multi-dimensional array" to describe a data-structure like: var multi = [[1,2,3],[4,5,6],[7,8,9]];. :-) I guess there's nothing stopping you from doing var multi = ['a',[1,2,3],'b',[4,5,6],'c',[7,8,9,10]]; :-P Commented Jul 10, 2013 at 15:56

1 Answer 1

2

I think what you want is some sort of lookup function like that I describe here, except modified slightly to take an Array instead of multiple arguments

function generateLookupFunction(o) {
    return function lookup(arr) {
        var i, e = o;
        for (i = 0; i < arr.length; ++i) {
            if (!e.hasOwnProperty(arr[i]))
                throw "PathNotFoundError: " + arr.slice(0, i + 1).join('/');
            e = e[arr[i]];
        }
        return e;
    };
}

Then

var lookup = generateLookupFunction(jsonTree),
    foo = ["customers","orders"];
lookup(foo);
/*[{
    "order": "1",
    "order": "2"
}] */
Sign up to request clarification or add additional context in comments.

Comments

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.