Skip to content

Instantly share code, notes, and snippets.

@ConradSulzer
Created February 9, 2021 22:54
Show Gist options
  • Save ConradSulzer/ac4d13da1ce7ce480e48bf808dc4fc7e to your computer and use it in GitHub Desktop.
Save ConradSulzer/ac4d13da1ce7ce480e48bf808dc4fc7e to your computer and use it in GitHub Desktop.
Query Object Recursively
//pathArray is an array where array[0] is the first property in the query path, array[1] the next property, etc.
//Particularly helpful if you have the path to the value you want in string form. You can use split() to get the array form the string
//If you may have the string 'food.type.apple'
//use split('.') to get the string path to the value want in array form and pass to the queryObject function
//obj is the object you want to run the query against
queryObject: function (pathArray, obj) {
//Takes path as an array and retreives the value
const [head, ...rest] = pathArray;
let value = ''
if (rest.length <= 0) {
value = obj[head];
return value
} else if (obj.hasOwnProperty(head)) {
value = app.queryObject(rest, obj[head])
return value
} else {
obj[head] = {};
value = app.queryObject(rest, obj[head])
return value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment