2

Is this possible to do, or how do I do it?

arrayElement = new Object();
JSONkey = jsonData.table[0].key; // key in table[0] is "ident/Lesson/Value"
JSONkey = JSONkey.replace(/\//g, '.'); // now JSONkey is "ident.Lesson.Value"

arrayElement.JSONkey = "value1" // Can I do this or how would I?

So arrayElement.JSONkey is the same as arrayElement.ident.Lesson.Value

Tim Harper
  • 2,561
  • 20
  • 23
DemiSheep
  • 698
  • 2
  • 15
  • 44
  • 1
    It's not clear what you're asking. What does `jsonData` look like? What do you want `arrayElement` to look like at the end? – Jordan Running Jan 26 '15 at 20:15
  • 1
    possible duplicate of [Accessing nested JavaScript objects with string key](http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key) – James Thorpe Jan 26 '15 at 20:18
  • There's a library for that called [jsonpath](https://github.com/jayway/JsonPath/blob/f0e055a3ba4a8c9966ae7f7cb9b21e10d99d05d9/json-path-web-test/src/main/resources/webapp/js/jsonpath-0.8.0.js). You might have to adapt your script to it's format, however. – Joseph Marikle Jan 26 '15 at 20:20
  • 1
    @JamesThorpe This question is about setting a nested value I think, so it's not really a duplicate. The question you pointed is about accessing. – Farid Nouri Neshat Jan 27 '15 at 16:35
  • 2
    @FaridNouriNeshat Fair enough. How about [this one as a dupe target then](http://stackoverflow.com/questions/10253307/setting-a-depth-in-an-object-literal-by-a-string-of-dot-notation)? – James Thorpe Jan 27 '15 at 16:44
  • 2
    Yes, that's the right one, I think I should improve my searching skills. Was looking for that :D – Farid Nouri Neshat Jan 27 '15 at 16:44
  • @JamesThorpe Thank you, yes I agree, duplicate - should I just delete my question or can I close it myself? I did vote to close it... – DemiSheep Jan 27 '15 at 17:53
  • 1
    @DemiSheep leave the question open, perhaps flag it as a duplicate. It will eventually get closed as a duplicate, but that's fine - search engines will still index it giving a broader range of terms for this sort of issue. If people end up here as a result of that, they'll get pointed at the other question – James Thorpe Jan 27 '15 at 18:02
  • @JamesThorpe Excellent point! – DemiSheep Jan 27 '15 at 18:04

1 Answers1

1
arrayElement = new Object();
JSONkey = jsonData.table[0].key; // key in table[0] is "ident/Lesson/Value"
JSONkey = JSONkey.replace(/\//g, '.'); // now JSONkey is "ident.Lesson.Value"

deepRef(arrayElement, JSONkey, "value1");

function deepRef(ref, key, value) {
    var segments = key.split("."),
        n = segments.length;
    for (var i=0, skey; i<n; i++) {
        skey = segments[i];
        if (i < n - 1) {
            ref[skey] = {};
            ref = ref[skey];
        } else {
            ref[skey] = value;
        }
    }
}
gontrollez
  • 6,372
  • 2
  • 28
  • 36
  • This does seem to work, thank you. However there is a more simplified version in the other link listed at the top of my question. – DemiSheep Jan 27 '15 at 19:17