Skip to content

Instantly share code, notes, and snippets.

@AnechaS
Last active April 25, 2023 15:01
Show Gist options
  • Select an option

  • Save AnechaS/2a6d52b1ec8fc92facaec226a75fa090 to your computer and use it in GitHub Desktop.

Select an option

Save AnechaS/2a6d52b1ec8fc92facaec226a75fa090 to your computer and use it in GitHub Desktop.

Revisions

  1. AnechaS revised this gist Apr 25, 2023. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions findPropertyPathWithValue.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,7 @@
    /**
    * Find property path in the object with a value
    *
    * @param {object} obj
    * @param {any} value
    *
    * @return {string}
    */
    function findPropertyPathWithValue(obj, value) {
  2. AnechaS revised this gist May 21, 2021. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions example.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +0,0 @@
    findPropertyPathWithValue(
    {
    firstname: {
    $inQuery: { className: "UserDetail", where: { firstname: "{{value}}" } },
    },
    },
    "{{value}}"
    ); // => firstname.$inQuery.where.firstname
  3. AnechaS renamed this gist May 21, 2021. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions example → example.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@

    // Example
    findPropertyPathWithValue(
    {
    firstname: {
  4. AnechaS revised this gist May 21, 2021. 2 changed files with 10 additions and 10 deletions.
    10 changes: 10 additions & 0 deletions example
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@

    // Example
    findPropertyPathWithValue(
    {
    firstname: {
    $inQuery: { className: "UserDetail", where: { firstname: "{{value}}" } },
    },
    },
    "{{value}}"
    ); // => firstname.$inQuery.where.firstname
    10 changes: 0 additions & 10 deletions findPropertyPathWithValue.js
    Original file line number Diff line number Diff line change
    @@ -18,13 +18,3 @@ function findPropertyPathWithValue(obj, value) {
    }
    }
    }

    // Example
    findPropertyPathWithValue(
    {
    firstname: {
    $inQuery: { className: "UserDetail", where: { firstname: "{{value}}" } },
    },
    },
    "{{value}}"
    ); // => firstname.$inQuery.where.firstname
  5. AnechaS created this gist May 21, 2021.
    30 changes: 30 additions & 0 deletions findPropertyPathWithValue.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    /**
    * Find property path in the object with a value
    *
    * @param {object} obj
    * @param {any} value
    *
    * @return {string}
    */
    function findPropertyPathWithValue(obj, value) {
    for (const key in obj) {
    if (JSON.stringify(value) === JSON.stringify(obj[key])) {
    return key;
    } else if (typeof obj[key] === 'object') {
    const pathname = findPropertyPathWithValue(obj[key], value);
    if (pathname) {
    return key + '.' + pathname;
    }
    }
    }
    }

    // Example
    findPropertyPathWithValue(
    {
    firstname: {
    $inQuery: { className: "UserDetail", where: { firstname: "{{value}}" } },
    },
    },
    "{{value}}"
    ); // => firstname.$inQuery.where.firstname