Complex JSON nesting of objects and arrays

前端 未结 5 1681
清歌不尽
清歌不尽 2020-12-23 09:51

I am having difficultly with syntax and structure of JSON objects/arrays.

{ 
  \"accounting\" : [   
                     { \"firstName\" : \"John\",  
              


        
5条回答
  •  被撕碎了的回忆
    2020-12-23 10:10

    You can try use this function to find any object in nested nested array of arrays of kings.

    Example

    function findTByKeyValue (element, target){
            var found = true;
            for(var key in target) { 
                if (!element.hasOwnProperty(key) || element[key] !== target[key])   { 
                    found = false;
                    break;
                }
            }
            if(found) {
                return element;
            }
            if(typeof(element) !== "object") { 
                return false;
            }
            for(var index in element) { 
                var result = findTByKeyValue(element[index],target);
                if(result) { 
                    return result; 
                }
            } 
        };
    
    findTByKeyValue(problems,{"name":"somethingElse","strength":"500 mg"}) =====> result equal to object associatedDrug#2
    

提交回复
热议问题