Answers for "get all keys of nested object json data javascript"

0

find key in nested json object

function getObject(theObject) {
    var result = null;
    if(theObject instanceof Array) {
        for(var i = 0; i < theObject.length; i++) {
            result = getObject(theObject[i]);
            if (result) {
                break;
            }   
        }
    }
    else
    {
        for(var prop in theObject) {
            console.log(prop + ': ' + theObject[prop]);
            if(prop == 'id') {
                if(theObject[prop] == 1) {
                    return theObject;
                }
            }
            if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
                result = getObject(theObject[prop]);
                if (result) {
                    break;
                }
            } 
        }
    }
    return result;
}
Posted by: Guest on January-08-2021
0

get all keys of nested object json data javascript

const getSchemaFields = (schema, result) => {
    if (Array.isArray(schema)) {
        for (var i = 0; i < schema.length; i++) {
            getSchemaFields(schema[i], result);
        }
    } else {
        for (var prop in schema) {
            if (['number'].includes(schema[prop].type)) {
                result.push(prop);
            }
            if (typeof schema[prop] == 'object' || Array.isArray(schema[prop])) {
                getSchemaFields(schema[prop], result);
            }
        }
    }
    return result;
}
const schema1 = {
    "amount": {
        "type": "number"
    },
    "invoice_url": {
        "type": "Object",
        "detail": {
            "type": "number"
        }
    }
}
const fields = getSchemaFields(schema1, []);
console.log(fields)
Posted by: Guest on April-07-2022

Code answers related to "get all keys of nested object json data javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language