0

I have a function that can get an array of an arbitary size, along with a value. The function should apply the keys given, to an object that has nested objects, and then set the last key to the value given. It then stores the object somewhere elsewhere.

This is easily done when the amount of keys is known, and consistent (object[key1][key2][key3] = value), however it seems impossible to do this when the amount of keys given is unknown.

I know I can't do something like this

let foo = object[stack[0]];
stack.slice(1).forEach(key => foo = foo[key]);
foo = value;

Because that will just end up reassigning foo.
What I want to do is to be able do something like object[stack] = value, or something similar.

EDIT
It seems that people are interpreting this as getting a nested value, which is not what I want. I want to set a nested value.

Example data for those that asked

let stack = ['a', 'b', 'c', 'd'];
let object = {
    a: {
        b: {
            c: {
                d: 'Lorem'
            }
        }
    }
}

// Do something that sets object.a.b.c.d to 'Lorem ipsum' (that isn't reliant on the number of keys)

console.log(object);
/*
    {
        a: {
            b: {
                c: {
                    d: 'Lorem ipsum'
                }
            }
        }
    }
*/
Ovyerus
  • 121
  • 2
  • 8
  • So you have an array of keys and you want some efficient way to apply each key consecutively to get the final nested value? – Andrew Li Nov 07 '17 at 03:56
  • This is a little confusing can you be a bit more clear? Maybe show us a bit more data or what you array might possibly look like that you are getting back – Enjayy Nov 07 '17 at 03:58
  • A little confusing question but I am guessing you would need to use recursive with this. I have had a similar problem that was solved by recursion. https://stackoverflow.com/questions/44103268/creating-hierarchy-using-id-and-parentid – Abana Clara Nov 07 '17 at 04:00
  • Edited question to include example data. – Ovyerus Nov 07 '17 at 04:42
  • Can you include sample output as well, what you would expect the output to be given the sample data. – dzm Nov 07 '17 at 04:49
  • Edited to include what the output should be. – Ovyerus Nov 07 '17 at 04:56

1 Answers1

0

So it turns out that I can't search as well as I think I can, and that I don't understand JavaScript pointers as well as I probably should.

I managed to find this answer which does exactly what I wish.

Simplified and edited version to deal with what I need

let stack = ['a', 'b', 'c', 'd'];
let object = {
    a: {
        b: {
            c: {
                d: 'Lorem'
            }
        }
    }
};
let ref = object; // This becomes a pointer to `object`.

// Loop through all the keys and change `ref` so that it becomes a pointer to each nested value.
stack.slice(0, -1).forEach(key => ref = ref[key]);

// Sets the last given key.
ref[stack.slice(-1)[0]] = 'Lorem ipsum';

console.log(object);
/*
    {
        a: {
            b: {
                c: {
                    d: 'Lorem ipsum'
                }
            }
        }
    }
*/
Ovyerus
  • 121
  • 2
  • 8