Skip to content

Instantly share code, notes, and snippets.

@m1ggy
Created June 17, 2022 13:13
Show Gist options
  • Select an option

  • Save m1ggy/7b6ecd7c89aae350724e2479abd7caa0 to your computer and use it in GitHub Desktop.

Select an option

Save m1ggy/7b6ecd7c89aae350724e2479abd7caa0 to your computer and use it in GitHub Desktop.

Revisions

  1. m1ggy created this gist Jun 17, 2022.
    20 changes: 20 additions & 0 deletions Object Flattener
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    //@source link [https://stackoverflow.com/questions/44134212/best-way-to-flatten-js-object-keys-and-values-to-a-single-depth-array]
    function flattenObject(ob) {
    var toReturn = {};

    for (var i in ob) {
    if (!ob.hasOwnProperty(i)) continue;

    if ((typeof ob[i]) == 'object' && ob[i] !== null) {
    var flatObject = flattenObject(ob[i]);
    for (var x in flatObject) {
    if (!flatObject.hasOwnProperty(x)) continue;

    toReturn[i + '.' + x] = flatObject[x];
    }
    } else {
    toReturn[i] = ob[i];
    }
    }
    return toReturn;
    }