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"]

Cœur
  • 37,241
  • 25
  • 195
  • 267
TMichel
  • 4,336
  • 9
  • 44
  • 67
  • possible duplicate of [JavaScript Multidimensional Arrays](http://stackoverflow.com/questions/2808926/javascript-multidimensional-arrays)...but I love the music references! JS does not do multidimensional arrays. – Derek Henderson Jul 10 '13 at 15:43
  • 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? – Tomalak Jul 10 '13 at 15:43
  • Do you want to create `jsonTree` or just select a value? Can't you just use a loop? `var ref = jsonTree; for(var i in foo){ if(foo.hasOwnProperty(i)){ ref = ref[foo[i]]; }}` – gen_Eric Jul 10 '13 at 15:44
  • @DerekHenderson: What do you mean by "JS does not do multidimensional arrays"? Yes it does. `var multi = [[1,2,3],[4,5,6],[7,8,9]];` – gen_Eric Jul 10 '13 at 15:45
  • 1
    Additionally, your sample object is not syntactically valid. Please set up a working sample on jsFiddle that demonstrates your problem. – Tomalak Jul 10 '13 at 15:46
  • @RocketHazmat That's not a multi-dimensional array. That's an array of arrays. – Tomalak Jul 10 '13 at 15:47
  • @Tomalak: Umm, what's the difference? – gen_Eric Jul 10 '13 at 15:47
  • @RocketHazmat, quoting directly from the accepted answer to the question I cited as a duplicate: "JavaScript does not have multidimensional arrays, but arrays of arrays, which can be used in a similar way." – Derek Henderson Jul 10 '13 at 15:48
  • @DerekHenderson: That doesn't really help. Are you trying to say that a multi-dimensional array and an array of arrays are two different things?! If so, what's the difference? I'm so confused. – gen_Eric Jul 10 '13 at 15:49
  • 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. – Tomalak Jul 10 '13 at 15:52
  • 1
    @RocketHazmat, to be honest, I wasn't really sure either, but my friend Google revealed the following to me: http://stackoverflow.com/questions/597720/what-is-differences-between-multidimensional-array-and-array-of-arrays-in-c. Although that question is specifically about C, the answers suffice for JS. – Derek Henderson Jul 10 '13 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 – gen_Eric Jul 10 '13 at 15:56
  • @Rocket: That's the point. :) – Tomalak Jul 10 '13 at 16:04
  • @Tomalak: Well, I learned something new today! :-D – gen_Eric Jul 10 '13 at 16:05

1 Answers1

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"
}] */
Community
  • 1
  • 1
Paul S.
  • 64,864
  • 9
  • 122
  • 138