I have an object with a name and a method with a name whose purpose is to create a new property for and a sub-property for the property or just skip it at all if it already exists. MyObject createEntry() MyObject
My code is:
var MyObject = {
createEntry: function (val1, val2, val3, val4) {
this[val1] = this[val1] || {};
this[val1][val2] = this[val1][val2] || {};
this[val1][val2][val3] = val4;
}
};
MyObject.createEntry("val1", "val2", "val3", "val4");
As shown in the above function, I am trying to create a new sub-object for each argument of the method , with the exception of the last two, where is this or and . createEntry() val3 property method val4
When my method is in its current state, I can only reach level 3 with its subsequent requirements requiring a longer and longer code. I assume that the above can be achieved with a loop , but I have not yet been able to understand it. while
:. - , , , :
var MyObject = {
val1: {
val2 {
val3: val4
}
}
}