Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js array map skip element 
Javascript :: javascript number to string 
Javascript :: jquery dom traversal parent 
Javascript :: flightphp 
Javascript :: jquery repeat event on click 
Javascript :: Get title assert 
Javascript :: case switch javascript 
Javascript :: js extract properties from object 
Javascript :: js key event 
Javascript :: how to delete a parameter with js 
Javascript :: array merge in javascript 
Javascript :: axios get request 
Javascript :: js dataset 
Javascript :: axios put api in componentDidMount React 
Javascript :: two days before in moment 
Javascript :: inline styling javascript 
Javascript :: max string size javascript 
Javascript :: how to add a tilemap in phaser 3 
Javascript :: react native measure 
Javascript :: javascript Recursionexample 
Javascript :: How to include route handlers in multiple files in Express 
Javascript :: react native add react native vector icons not working 
Javascript :: js immediately invoked function 
Javascript :: eslint ignore javascript 
Javascript :: return two arrays javascript 
Javascript :: ngShow 
Javascript :: getting started with react 
Javascript :: hasChildNodes 
Javascript :: create an express application 
Javascript :: what is my version of linux mint 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =