@nevware21/ts-utils
    Preparing search index...

    Function setValueByIter

    • Set the named value on the target object where the path is represented by the string iterator or iterable to separate the nested objects of the heirarchy / path to the value.

      For safety, this helper blocks setting any path that includes unsafe keys (__proto__, constructor, or prototype) and will also stop if the traversal reaches an unsafe target such as a built-in prototype object, without writing the final value. A missing (null or undefined) value of the path is replaced by a new object, as is (since 0.20.0) an inherited function (eg. the hasOwnProperty of an object or a method of a class), which is added as an own property of the target, so the path sets the target's own properties rather than changing an inherited (eg. built-in) function, eg. ["hasOwnProperty", "call"] sets the target's own hasOwnProperty to { call: value }. Each key is converted to a property key (a string, unless it's a symbol) once, like a property access, before it is checked and used (except that a key which isn't a symbol but converts to one, eg. a boxed Symbol, throws a TypeError). Any objects which were added for the path are removed again (or the own null or undefined value they replaced is restored) when the value is not set (unless one was passed to an inherited setter), including when the iterator (or its return(), which like for...of must return an object) throws.

      The order of processing of the iterator is not reset if you add or remove elements to the iterator, the actual behavior will depend on the iterator imeplementation.

      If the passed iter is both an Iterable and Iterator, like for...of (and iterForOf) the iterator returned by its [Symbol.iterator]() is used.

      The path is read like iterForOf, so on a runtime without Symbol.iterator (eg. IE) an array, an iterator (or an iterable created by this library) and a Map or Set are read, but a value which is only iterable where there is Symbol.iterator (eg. an arguments object) has no keys, so nothing is set. Pass an array (eg. using arrFrom) to use the same path on every runtime.

      Type Parameters

      • T

      Parameters

      • target: any

        The target object

      • iter: Iterator<string, any, undefined> | Iterable<string>

        The iter identifying the path of the final key value

      • value: T

        The value to set

      Returns void

      0.9.1

      TypeError when the iter's [Symbol.iterator] isn't null, undefined or a function, its [Symbol.iterator]() doesn't return an iterator, the iterator's next() returns a value which is not an object, or (like for...of) when it stops early (the path is not valid) and the iterator's return() doesn't return an object

      let theValue = { };
      setValueByIter(theValue, ["Hello", "Darkness", "my"], "old");
      // Resulting Object: { Hello: { Darkness: { my: "old" } } }
      setValueByIter(theValue, ["friend"], "I've");
      // Resulting Object: { Hello: { Darkness: { my: "old" } }, friend: "I've" }
      setValueByIter(theValue, ["come", "to", "see"], "you");
      // Resulting Object: { Hello: { Darkness: { my: "old" } }, friend: "I've", come: { to : { see: "you" } } }