JavaScript reducer that updates a deep value in a hash I've been roughing out a proof of concept enabling admin staff to flag submitted applications with issues as needed. We have many forms some of which are nested. So we end up with these big hashes to validate. Primarily we have issues with uploaded images (too blurry, too dark etc) but it could be any field.

So admin may see a field verified_documents -&gt; drivers_licence -&gt; front_image. If an issue is raised we end up with a hash to store.

{
    verified_documents: {
        drivers_licence: {
            front_image: ['Image is too blurry', 'Image is too dark']
        }
    }
}


The code works but is very smelly. The function in question is updateIssue. Which accepts a string representation of the path "verified_documents.drivers_licence.front_image" and an array of selected options from a dropdown.

The function's intentions:


Take a copy of current issues, an attempt to be pure and return a new object.
Split the string into an array.
Iterate over each value in the path array.
If path length is the last set its value to the array of issues.
If the value is an empty array set the value to undefined (only happens if an issue is removed).
If the key can't be found, create the next level as a child hash.
Returning the next level of nested object if no other condition is met.


I'm happy I was able to get it all working, but now it's not readable. Readability over performance is my objective, as this code is likely to change. 

import { merge, isEmpty } from "lodash";

let Issues = undefined;

const getNestedValue = (nestedObj, pathArr) =&gt; {
  return pathArr.reduce(
    (obj, key) =&gt; (obj &amp;&amp; obj[key] !== "undefined" ? obj[key] : undefined),
    nestedObj
  );
};

export class IssuesData {
  static updateIssue = (path, value) =&gt; {
    const valueObj = merge({}, Issues);
    const pathArr = path.split(".");
    pathArr.reduce((obj, key, i) =&gt; {
      if (pathArr.length - 1 === i) {
        if (isEmpty(value)) {
          return (obj[key] = undefined);
        }
        return (obj[key] = value);
      }

      if (obj[key] === undefined) {
        return (obj[key] = {});
      }
      return obj[key];
    }, valueObj);
    Issues = valueObj;
  };

  static getIssueByKey = path =&gt; {
    return getNestedValue(Issues, path.split("."));
  };

  static getIssues() {
    return Issues;
  }

  static setIssues(issues) {
    if (!Issues) {
      Issues = issues;
    }
  }
}




UPDATE:

After sleeping on it I managed to clean it up a little. This is more readable but there are still multiple return. If anyone has advice on cleaning it up further it'll be welcomed. I think I'll turn my attention to converting the static methods to instance methods now. 

  static updateIssue = (path, value) =&gt; {
    const valueObj = merge({}, Issues);
    const pathArray = path.split(".");

    pathArray.reduce((obj, key, i) =&gt; {
      if (isLast(pathArray, i)) {
        if (isEmpty(value)) {
          return delete obj[key];
        }
        return (obj[key] = value);
      }
      return obj[key];
    }, valueObj);
    Issues = valueObj;
  };
Since you are already using lodash, don't reinvent the wheel! Nearly all of your code can be replaced by a single _.set call.



const Issues = {
  verified_documents: {
    drivers_licence: {
      front_image: ['Image is too blurry', 'Image is too dark']
    }
  }
}

console.log(Issues)
_.set(Issues, 'verified_documents.drivers_licence.front_image', [])
console.log(Issues)
.as-console-wrapper { max-height: 100% !important; }
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"&gt;&lt;/script&gt;




With that said, I believe there is merit to reviewing your approach.


Not mutating an object doesn't mean a function is pure. Sure, updateIssue doesn't mutate the Issues object, but it does overwrite it, making this function inherently impure.
Don't use merge to clone an object. Use clone or cloneDeep.
A class with nothing but static functions shouldn't be a class. Just use a plain object, or if you are using a proper module system, don't even bother wrapping functions in an object!
setIssues worries me. When I call a method called setSomething, I expect it to either set, or throw an error. Silently ignoring the call if Issues is already instantiated will probably lead to painful debugging later.
getNestedValue can be replaced with at.
Multiple returns is not necessarily a bad thing. The "single exit" rule is far more important when writing assembly than JavaScript. There's plenty of discussion on this. See this blog, another post, this software engineering question... plenty more to be found with a simple search.
getNestedValue looks like it has a bug, obj[key] !== "undefined" should probably have a typeof in front of it. 
Your rewritten function, while cleaner, does not exactly replicate the behavior of your original implementation. If a key that does not exist in the object is specified, the original implementation will set it. The refactored function will throw.