json JSLT -是否存在全局变量?

ru9i0ody  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(133)

我正在循环一个for循环,我想把最小值存储在一个String中。但是当我使用let关键字时,我认为它是在创建一个局部变量。有没有一种方法可以在for循环中为函数内的全局变量赋值。

let ranking = 
{ 
 "DEC": 0,
 "SBS" : 1,
 "CON" : 2,
 "GCS" : 3,
 "GMS" : 4,
 "FXP" : 5,
 "QAN" : 6, 
 "REF" : 7,
 "PRO" : 8
}

let minRankSegment = "DEC"

def findMinRankSegment(segment)
  if(get-key($ranking, $segment, 100)  <= get-key($ranking, $minRankSegment))
    $segment
  else 
    let minRankSegment = $segment
    $minRankSegment
  

{for (.payload) 

  "data" : {
    "segment" :findMinRankSegment(.tag)
   }
}

下面是我的输入

{ "payload" : 
   [
    {"tag":"ABC"},
    {"tag":"DEC"},
    {"tag":"QAN"}
   ]
}

我期望输出为

{
  "data" : {
    "segment" : "DEC",
  }
}

但事实如此

{
  "data" : {
    "segment" : "QAN",
  }
}
s4n0splo

s4n0splo1#

我想你可以用这个:

let ranking = 
{
 "DEC": 0,
 "SBS": 1,
 "CON": 2,
 "GCS": 3,
 "GMS": 4,
 "FXP": 5,
 "QAN": 6, 
 "REF": 7,
 "PRO": 8
}

let minRankSegment = "DEC"

let minSegment = [for (.payload)
  .tag
  if(get-key($ranking, .tag, 100) <= get-key($ranking, $minRankSegment))
]

{
  "data": {
    "segment": $minSegment[0]
   }
}

我把for循环的对象改为array for循环,然后得到tag,而你的if表达式是true

相关问题