Function getValueByKey

  • Get the named value from the target object where the path may be presented by a string which contains "." characters to separate the nested objects of the heirarchy / path to the value.

    Type Parameters

    • V
    • T extends object = any

    Parameters

    • target: T

      The source object that contains the value

    • path: string

      The path identifing the location where the value should be located

    • Optional defValue: 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

    Since

    0.9.1

    Example

    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"