[英]Loop through an same object in js
{
"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这是一个 object,每个都有“树”,我需要获取长度并循环遍历它
Assuming your object is stored in a variable $a
, then $a['tree']
will give you the 'tree' array.假设您的 object 存储在变量$a
中,那么$a['tree']
将为您提供 'tree' 数组。 You can use forEach to loop through it: $a['tree'].forEach()
(no need to determine the length).您可以使用forEach循环遍历它: $a['tree'].forEach()
(无需确定长度)。
If you need the length , use $a['tree'].length
.如果您需要长度,请使用$a['tree'].length
。
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]);
You need a recursive dig
function?您需要递归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.如果您熟悉的话,它类似于 PHP 中的array_walk_recursive
。 Of course, this function takes Objects whether they are instanceof Array
or not.当然,这个 function 无论是否是instanceof Array
都接受对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.