JSFiddle - React, Tailwind, and code Playground

by Hari Menon

JavaScript

// http://stackoverflow.com/questions/1988349/array-push-if-does-not-exist
// check if an element exists in array using a comparer function
// comparer : function(currentElement)
Array.prototype.inArray = function(comparer) {
    for (var i = 0; i < this.length; i++) {
        if (comparer(this[i])) return true;
    }
    return false;
};

// adds an element to the array if it does not already exist using a comparer function
Array.prototype.pushIfNotExist = function(element, comparer) {
    if (!this.inArray(comparer)) {
        this.push(element);
    }
};

var keys = [],
    index = 0,
    _extractKeys = function(obj) {
        if (typeof obj === 'object') {
            for (var key in obj) {
                if (!Array.isArray(obj)) {
                    keys.push(key);
                    keys.pushIfNotExist(key, function(e) {
                        return e === key;
                    });
                }
                _extractKeys(obj[key]);
            }
        }
    },
    obj = [
        {
        mykey1: "value1",
        mykey2: [
            {
            subkey1: "subvalue1",
            subkey2: null,
            subkey3: {
                subsubkey1: "subsubvalue1"
            }},
        {
            subkey1: "subvalue12",
            subkey2: null,
            subkey3: {
                subsubkey1: "subsubvalue12"
            }}],
        mykey3: "string One",
        mykey4: 56.71},
    {
        mykey1: "value2",
        mykey2: [{
            subkey1: "subvalue2",
            subkey2: "somevalue",
            subkey3: {
                subsubkey1: "subsubvalue2"
            }}],
        mykey3: "string Two",
        mykey4: 56.72}
    ];
_extractKeys(obj);
alert(JSON.stringify(keys));