truncateToArrayDepth = function (key, arrayDepth) {
  var depth = 0;
  var pos = 0;
  if (!key) return null;

  if (arrayDepth > 0) {
    while (depth < arrayDepth) {
      pos = key.indexOf('[]', pos);
      if (pos === -1) {
        // Key path is not "deep" enough, simply return the full key
        return key;
      }
      pos = pos + 2;
      depth += 1;
    }
  }

  // Move one step further to the right without including the final []
  pos = key.indexOf('[]', pos);
  if (pos === -1) return key;
  else return key.substring(0, pos);
}
Truncates the key path to the requested depth.

For instance, if the key path is:
 foo.bar[].baz.toto[].truc[].bidule
and the requested depth is 1, the returned key will be:
 foo.bar[].baz.toto

Note the function includes the path up to the next depth level.

@function
@param {String} key The path to the key in the schema, each level being
 separated by a dot and array items being flagged with [].
@param {Number} depth The array depth
@return {String} The path to the key truncated to the given depth.