diff --git a/src/tasks/helpers/saveContainer.js b/src/tasks/helpers/saveContainer.js index eb6c07b..a0ee4a2 100644 --- a/src/tasks/helpers/saveContainer.js +++ b/src/tasks/helpers/saveContainer.js @@ -80,24 +80,32 @@ const setValueToObject = (obj, propertyPath, value) => { return true; }; -const deleteValueFromObject = (obj, propertyPath) => { - let parentObj = null; - let parentKey = null; +const deleteValueFromObject = (obj, propertyPath, i = 0) => { + const key = propertyPath[i]; - for (let i = 0; i < propertyPath.length; i += 1) { - const key = propertyPath[i]; - if (!obj[key]) { + if (i === propertyPath.length - 1) { + if (!obj[propertyPath[i]]) { return false; } + delete obj[propertyPath[i]]; + return true; + } - parentObj = obj; - parentKey = key; - obj = obj[key]; + if (propertyPath[i].endsWith('[]')) { + const arrayKey = key.substring(0, key.length - 2); + if (!obj[arrayKey] || !Array.isArray(obj[arrayKey])) { + return false; + } + return obj[arrayKey].reduce((memo, subObj) => { + return deleteValueFromObject(subObj, propertyPath, i + 1) || memo; + }, false); } - delete parentObj[parentKey]; + if (!obj[propertyPath[i]]) { + return false; + } - return true; + return deleteValueFromObject(obj[propertyPath[i]], propertyPath, i + 1); }; const functionTransform = (transformData, fnCode) => {