Created
February 1, 2017 03:40
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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:
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.