Skip to content

Instantly share code, notes, and snippets.

@gr0uch
Last active June 27, 2016 15:18
Show Gist options
  • Select an option

  • Save gr0uch/e01c035c4b2ec3f2da1e74e37530ff35 to your computer and use it in GitHub Desktop.

Select an option

Save gr0uch/e01c035c4b2ec3f2da1e74e37530ff35 to your computer and use it in GitHub Desktop.

Revisions

  1. 0x8890 revised this gist Jun 27, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion walk_path.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    * ```
    *
    * @param {Object} obj
    * @param {String[]|Number[]} path
    * @param {String|Number[]} path
    * @return {*}
    */
    function walk (obj, path) {
  2. 0x8890 revised this gist Jun 17, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion walk_path.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function walk (obj, path) {

    for (i = 0, j = path.length; i < j; i++) {
    key = path[i]
    if (result !== null && typeof result === 'object' && key in result)
    if (typeof result === 'object' && result !== null && key in result)
    result = result[key]
    else {
    result = void 0
  3. 0x8890 revised this gist Jun 17, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion walk_path.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,8 @@ function walk (obj, path) {

    for (i = 0, j = path.length; i < j; i++) {
    key = path[i]
    if (key in result) result = result[key]
    if (result !== null && typeof result === 'object' && key in result)
    result = result[key]
    else {
    result = void 0
    break
  4. 0x8890 created this gist Jun 17, 2016.
    27 changes: 27 additions & 0 deletions walk_path.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    /**
    * Given an object and an array of keys, walk over the keys on the object.
    *
    * For example:
    * ```js
    * walk({ a: { b: [ 0, { c: 2 } ] } }, [ 'a', 'b', 1, 'c' ]) // returns 2
    * ```
    *
    * @param {Object} obj
    * @param {String[]|Number[]} path
    * @return {*}
    */
    function walk (obj, path) {
    var result = obj
    var i, j, key

    for (i = 0, j = path.length; i < j; i++) {
    key = path[i]
    if (key in result) result = result[key]
    else {
    result = void 0
    break
    }
    }

    return result
    }