Function getValueByIter

  • Get the named value from 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. If the target does not contain the full path the iterator will not be completed.

    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 the Iterator interface takes preceedence.

    Type Parameters

    • V
    • T extends object = any

    Parameters

    • target: T

      The source object that contains the value

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

      The iter identifying the path of the final key value

    • OptionaldefValue: V

      If the final value or any intervening object in the heirarchy is not present the default value will be returned

    Returns V

    The value located based on the path or the defaule value

    0.9.1

    let theValue = {
    Hello: {
    Darkness: {
    my: "old"
    }
    },
    friend: "I've",
    come: {
    to: {
    see: "you"
    }
    }
    };

    let value = getValueByKey(theValue, ["Hello", "Darkness", "my"], "friend");
    // value === "my"

    let value = getValueByKey(theValue, ["My", "Old"], "friend");
    // value === "friend"

    let value = getValueByKey(theValue, ["come", "to"], "friend");
    // value === { see: "you" }

    let value = getValueByKey(theValue, ["friend"], "friend");
    // value === "I've"