Good day! I wanted to simplify work with input when forming a multi-level object, to be sent to the server. The idea was simple, input names set such that would correspond to the keys of a multi-level object. For example: <input name="price.now"> <input name="price.past"> <input name="price.past.first"> , etc. I asked the appropriate question. Synchronize input with a multi-level object . But as it turned out, there are no standard methods for such an implementation. I tried to write my own universal function for the implementation of our plans. It seems to have happened.

See the answer - point out the shortcomings of my code, and also suggest your options for implementation.

    2 answers 2

    I think it will be a little easier:

     var startObject = {}; var inputName = "price.past.first"; var inputName2 = "price.past.second"; var inputName3 = "price.now"; var inputValue = 10; var inputValue2 = 20; var inputValue3 = 30; setObject(startObject, inputName, inputValue); setObject(startObject, inputName2, inputValue2); setObject(startObject, inputName3, inputValue3); console.log(startObject); function setObject(startObject, string, value) { let path = string.split('.'); let curObj = startObject; for (let i = 0; i < path.length - 1; i++) { if (!curObj[path[i]]) curObj[path[i]] = {}; curObj = curObj[path[i]]; } curObj[path[path.length - 1]] = value; } 

    • For some reason I decided that asynchrony could interfere with the implementation) - Dmytryk

     var startObject= {}; var inputName = "price.past.first"; var inputName2 = "price.past.second"; var inputName3 = "price.now"; var inputValue = 10; var inputValue2 = 20; var inputValue3 = 30; setObject(startObject,inputName, inputValue); setObject(startObject,inputName2, inputValue2); setObject(startObject,inputName3, inputValue3); console.log(startObject); //функция принимает объект, которому необходимо установить значение, //путь и само значение function setObject(startObject, string, value) { var arrString = string.split("."); startCicleFunc(arrString); function startCicleFunc(arr){ (function step(object, i){ //проверка последний это ключ или нет if (i == arr.length-1){ setValue(object, arr[i], value, function(objNext){ step (objNext, i+1); }); //условие выхода из рекурсии }else if (i == arr.length){ return; } else { setKey(object, arr[i], function(objNext){ step (objNext, i+1); }); } })(startObject, 0); //создает вложенность объекту function setKey(obj, key, callback){ if (key in obj){ return callback(obj[key]); } else { obj[key] = {}; return callback(obj[key]); } } //присваивает значение ключу объекта function setValue(obj, key, value, callback){ obj[key]= value ; return callback(obj); } } } 

    Based on the previous function, I made the function of getting the value by line:

     var startObject= { price:{ past:{ first:10, second:{ one:40, two:50 }, }, now:30, }, }; var inputName = "price.past.first"; var inputName2 = "price.now"; var inputName3 = "price.past.second"; function getObjectValue(startObject, string) { var arrString = string.split("."); var c; startCicleFunc(arrString); function startCicleFunc(arr){ (function step(object, i){ if (i == arr.length-1){ getValue(object, arr[i], function(resultValue){ return c = resultValue; }); }else { getKey(object, arr[i], function(objNext,){ step (objNext, i+1); }); } })(startObject, 0); function getKey(obj, key, callback){ if (key in obj){ return callback(obj[key]); } else { console.log("nen") return c; } } function getValue(obj, key, callback){ return callback(obj[key]); } } return c; } console.log(getObjectValue(startObject, inputName)); console.log(getObjectValue(startObject, inputName2)); console.log(getObjectValue(startObject, inputName3)); 

    • The disadvantage here (as well as in my version) is that if you set inputName3 = "price.past" , then the length values ​​of inputName and inputName2 will inputName2 lost irretrievably. - Yaant
    • @Yaant, well, in principle, the same thing will happen if you assign a value to an object directly: object.price.past = 10 , Ten will overwrite any, previous, key value. So, I would refer it not to the problem of implementation, but already to the logic of use - Dmytryk
    • Yes, but here the situation is slightly worse in the sense that if you change the order of calls: setObject(startObject,inputName3, inputValue3); setObject(startObject,inputName, inputValue); setObject(startObject,inputName2, inputValue2);' то setObject(startObject,inputName3, inputValue3); setObject(startObject,inputName, inputValue); setObject(startObject,inputName2, inputValue2);' то setObject(startObject,inputName3, inputValue3); setObject(startObject,inputName, inputValue); setObject(startObject,inputName2, inputValue2);' то inputName` and inputName2 will disappear anyway. - Yaant