-1
{
    "uri": "https://www.fsa.go.jp/en/newsletter/index.html",
    "path": "D:\\SSN_INS\\JP--FSA--INS--NEWSLETTER\\",
    "pattern": "/.+/",
    "scope": "#main",
    "selector": "void",
    "extractors": [
        {
            "name": "TitleHtml",
            "query": {
                "scope": "title"
            }
        },
        {
            "name": "Title",
            "query": {
                "scope": "title"
            }
        }
    ],
    "tree": [
        {
            "type": "pseudo",
            "scope": ":scope .col-two_fp > div h2",
            "extractors": [
                {
                    "name": "Title",
                    "query": {
                        "scope": ":scope"
                    }
                },
                {
                    "name": "TitleHtml",
                    "query": {
                        "scope": ":scope"
                    }
                }
            ],
            "tree": [
                {
                    "type": "pseudo",
                    "scope": ":scope ~ h3",
                    "extractors": [
                        {
                            "name": "Title",
                            "query": {
                                "scope": ":scope",
                                "exclude": "span:has(a)"
                            }
                        },
                        {
                            "name": "TitleHtml",
                            "query": {
                                "scope": ":scope",
                                "exclude": "span:has(a)"
                            }
                        }
                    ],
                    "tree": [
                        {
                            "type": "pseudo",
                            "scope": ":scope ~ h4",
                            "extractors": [
                                {
                                    "name": "Title",
                                    "query": {
                                        "scope": ":scope",
                                        "exclude": "span:has(a)"
                                    }
                                },
                                {
                                    "name": "TitleHtml",
                                    "query": {
                                        "scope": ":scope",
                                        "exclude": "span:has(a)"
                                    }
                                }
                            ]

                        }
                    ]

                }
            ]
        }
    ]
}

This is an object has 'tree' for each and I need to get the length and loop through it

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64

3 Answers3

0

Assuming your object is stored in a variable $a, then $a['tree'] will give you the 'tree' array. You can use forEach to loop through it: $a['tree'].forEach() (no need to determine the length).

If you need the length, use $a['tree'].length.

kmoser
  • 8,780
  • 3
  • 24
  • 40
0

May use recursion if expected tree length is not very large.

var data = {
    "uri": "https://www.fsa.go.jp/en/newsletter/index.html",
    "path": "D:\\SSN_INS\\JP--FSA--INS--NEWSLETTER\\",
    "pattern": "/.+/",
    "scope": "#main",
    "selector": "void",
    "extractors": [
        {
            "name": "TitleHtml",
            "query": {
                "scope": "title"
            }
        },
        {
            "name": "Title",
            "query": {
                "scope": "title"
            }
        }
    ],
    "tree": [
        {
            "type": "pseudo",
            "scope": ":scope .col-two_fp > div h2",
            "extractors": [
                {
                    "name": "Title",
                    "query": {
                        "scope": ":scope"
                    }
                },
                {
                    "name": "TitleHtml",
                    "query": {
                        "scope": ":scope"
                    }
                }
            ],
            "tree": [
                {
                    "type": "pseudo",
                    "scope": ":scope ~ h3",
                    "extractors": [
                        {
                            "name": "Title",
                            "query": {
                                "scope": ":scope",
                                "exclude": "span:has(a)"
                            }
                        },
                        {
                            "name": "TitleHtml",
                            "query": {
                                "scope": ":scope",
                                "exclude": "span:has(a)"
                            }
                        }
                    ],
                    "tree": [
                        {
                            "type": "pseudo",
                            "scope": ":scope ~ h4",
                            "extractors": [
                                {
                                    "name": "Title",
                                    "query": {
                                        "scope": ":scope",
                                        "exclude": "span:has(a)"
                                    }
                                },
                                {
                                    "name": "TitleHtml",
                                    "query": {
                                        "scope": ":scope",
                                        "exclude": "span:has(a)"
                                    }
                                }
                            ]

                        }
                    ]

                }
            ]
        }
    ]
}

function findObjectByLabel(node, key, childList) {
    if(!childList)
        childList = [];
        
    if(node.hasOwnProperty(key)) {
        node = node[key][0];
        childList.push(node);
        return findObjectByLabel(node, key, childList);
    }
    return childList;
}
 
var childList = findObjectByLabel(data, "tree");
for(var i=0; i < childList.length; ++i)
    console.log(childList[i]);
Miller Cy Chan
  • 897
  • 9
  • 19
0

You need a recursive dig function?

function dig(obj, func){
  let v;
  if(obj instanceof Array){
    for(let i=0,l=obj.length; i<l; i++){
      v = obj[i];
      if(typeof v === 'object'){
        dig(v, func);
      }
      else{
        func(v, i);
      }
    }
  }
  else{
    for(let i in obj){
      v = obj[i];
      if(typeof v === 'object'){
        dig(v, func);
      }
      else{
        func(v, i);
      }
    }
  }
}
const data = {
    "uri": "https://www.fsa.go.jp/en/newsletter/index.html",
    "path": "D:\\SSN_INS\\JP--FSA--INS--NEWSLETTER\\",
    "pattern": "/.+/",
    "scope": "#main",
    "selector": "void",
    "extractors": [
        {
            "name": "TitleHtml",
            "query": {
                "scope": "title"
            }
        },
        {
            "name": "Title",
            "query": {
                "scope": "title"
            }
        }
    ],
    "tree": [
        {
            "type": "pseudo",
            "scope": ":scope .col-two_fp > div h2",
            "extractors": [
                {
                    "name": "Title",
                    "query": {
                        "scope": ":scope"
                    }
                },
                {
                    "name": "TitleHtml",
                    "query": {
                        "scope": ":scope"
                    }
                }
            ],
            "tree": [
                {
                    "type": "pseudo",
                    "scope": ":scope ~ h3",
                    "extractors": [
                        {
                            "name": "Title",
                            "query": {
                                "scope": ":scope",
                                "exclude": "span:has(a)"
                            }
                        },
                        {
                            "name": "TitleHtml",
                            "query": {
                                "scope": ":scope",
                                "exclude": "span:has(a)"
                            }
                        }
                    ],
                    "tree": [
                        {
                            "type": "pseudo",
                            "scope": ":scope ~ h4",
                            "extractors": [
                                {
                                    "name": "Title",
                                    "query": {
                                        "scope": ":scope",
                                        "exclude": "span:has(a)"
                                    }
                                },
                                {
                                    "name": "TitleHtml",
                                    "query": {
                                        "scope": ":scope",
                                        "exclude": "span:has(a)"
                                    }
                                }
                            ]

                        }
                    ]

                }
            ]
        }
    ]
}
dig(data, (v, i)=>{
  console.log({key:i, value:v});
});

It's similar to array_walk_recursive in PHP, if you're familiar. Of course, this function takes Objects whether they are instanceof Array or not.

StackSlave
  • 10,613
  • 2
  • 18
  • 35