Skip to content

Instantly share code, notes, and snippets.

@tjdaley
Created February 1, 2017 03:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tjdaley/119181bdda524d01093d6a8ac633d7be to your computer and use it in GitHub Desktop.
Save tjdaley/119181bdda524d01093d6a8ac633d7be to your computer and use it in GitHub Desktop.
Get or Set an object's property value based on a dot-notation key
/*
* dot-notation-setter-getter.js
*
* Created by Thomas J. Daley, J.D. on Jan 31, 2017
*
*/
"use strict";
/**
* Get a value based on a dot-notation key.
*
* @param {{}} obj - Object to be read from
* @param {string | [string]} keys - Initially, the dot-notation key.
* @returns {*} - Value sought OR undefined if key does not exist in obj
*/
function getValue(obj, keys)
{
keys = (typeof keys === "string") ? keys.split(".") : keys;
const key = keys.shift();
if (obj.hasOwnProperty(key) && keys.length === 0)
return obj[key];
else if (!obj.hasOwnProperty(key))
return undefined;
else
return getValue(obj[key], keys);
}
/**
* Set a value based on a dot-notation key.
*
* @param obj
* @param keys
* @param value
*/
function setValue(obj, keys, value)
{
keys = (typeof keys === "string") ? keys.split(".") : keys;
const key = keys.shift();
if (keys.length === 0)
{
obj[key] = value;
return;
}
else if (!obj.hasOwnProperty(key))
{
obj[key] = {};
}
setValue(obj[key], keys, value)
}
/*
* T E S T / D E M O
*/
let obj = {};
setValue(obj, "userProfile.ids.DL.state", "AR");
setValue(obj, "userProfile.ids.DL.id", "123");
setValue(obj, "userProfile.ids.SSN.id", "6692");
setValue(obj, "userProfile.name.fullName", "Thomas J. Daley");
setValue(obj, "userProfile.name.firstname", "Thomas");
setValue(obj, "userProfile.name.middleName", "J.");
setValue(obj, "userProfile.name.lastName", "Daley");
console.log(JSON.stringify(obj));
//{"userProfile":{"ids":{"DL":{"state":"AR","id":"123"},"SSN":{"id":"6692"}},"name":{"fullName":"Thomas J. Daley","firstname":"Thomas","middleName":"J.","lastName":"Daley"}}}
console.log(getValue(obj, "userProfile.name.fullName"));
//Thomas J. Daley
@tjdaley
Copy link
Author

tjdaley commented Feb 1, 2017

Get/Set Object Property Using Dot-Notation

This is motivated by jdbot's need to save user responses to particular locations in a userProfile object. For example, we can have a configuration object like this:

let config =
{
    "prompt": "What is your first name?",
    "saveTo": "userProfile.name.firstName"
};

Then we can prompt for the value like this (using Microsoft's botbuilder framework, which is the context of my use).

builder.Prompts.text(session, config.prompt);

and when we get the result back we can save it like this:

function (session, results, next)
{
    setValue(session.userData, config.saveTo, results.response);
    session.send("Hello, %s", getValue(session.userData, config.saveTo);
    next();
}

This might not seem like much, but imagine a Dialog that processes an array of config objects and that is completely configurable from a database or json-formatted file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment