我试图在if语句中使用return破坏函数。我认为我不会停止该循环,而是使另一个循环不再返回。但我找不到解决该问题的方法。
svg{border:1px solid}
<svg id="theSVG" viewBox="0 0 30 30" width="300">
<g id="theG">
<rect x="20" y="0" width="100" height="20" transform="rotate(45)" fill="black" />
</g>
</svg>
此函数应该获取一个对象并将路径返回到所需值。
答案 0 :(得分:0)
首先,您输入的对象是错误的,您在最外层有一个字典,但是缺少键,所以我假设您的对象是一个像这样的列表
b = [
{
"id": "160407",
"created": "2017-10-30T09:41:37.960+0000",
"items": [
{
"field": "status",
"fieldtype": "test",
"from": "10407",
"fromString": "Analysis",
"to": "4",
"toString": "To Do"
}
]
},
{
"id": "160407",
"created": "2019-10-30T09:41:37.960+0000",
"items": [
{
"field": "status",
"fieldtype": "test",
"from": "10407",
"fromString": "Analysis",
"to": "3",
"toString": "In Progress"
}
]
}
]
要忽略返回值的复杂性,使用全局变量要容易得多,在达到条件并分配该变量并将其打印出来时
result = []
def recursive(obj, path=None):
global result
if path is None:
path = []
# Check for object type and unpacked
if isinstance(obj, dict):
for key, value in obj.items():
# Write path for this key
new_path = list(path)
new_path.append(key)
# Check Status = in progress
condition = 'to' in obj and '3' == obj['to']
if condition:
result = new_path
return new_path #None
recursive(value, path=new_path)
# Check for list type and unpacked
elif isinstance(obj, list):
for i, item in enumerate(obj):
new_path = list(path)
new_path.append(i)
recursive(item, path=new_path)
recursive(b) #None
print(result)
#[1, 'items', 0, 'field']