从嵌套对象参数中检索值

发布于 2025-01-21 00:48:12 字数 2060 浏览 0 评论 0原文

我的问题密切相关,但我似乎没有帮助我:( 如何从嵌套对象检索值?

我正在尝试循环浏览我作为参数的对象,我以默认值为例,该对象中的值也是对象。

我有一个模板:

parameters: 
  - name: fruits
    type: object
    default:     
      - Banana:
          name: " acuminata"
          url: "www.banana.com/ acuminata"
      - Apple:
          name: "jonaGold"
          url: "www.apple.com"


steps:

- bash: echo ${{parameters.fruits[0].url}}    



- ${{ each fruit in parameters.fruits }}:
  - script: echo ${{fruit.name}}

第二个bash均可使用。

第一个bash和

2022-04-13T13:01:21.8035586Z ##[section]Starting: CmdLine
2022-04-13T13:01:21.8178179Z ==============================================================================
2022-04-13T13:01:21.8178533Z Task         : Command line
2022-04-13T13:01:21.8178874Z Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
2022-04-13T13:01:21.8179171Z Version      : 2.201.1
2022-04-13T13:01:21.8179614Z Author       : Microsoft Corporation
2022-04-13T13:01:21.8179954Z Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
2022-04-13T13:01:21.8180355Z ==============================================================================
2022-04-13T13:01:23.2732262Z Generating script.
2022-04-13T13:01:23.2840010Z Script contents: shell
2022-04-13T13:01:23.2849739Z echo
2022-04-13T13:01:23.3217106Z ========================== Starting Command Output ===========================
2022-04-13T13:01:23.3517504Z ##[command]"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\7a91b1cc-432d-4a20-b39c-7c6b0c78724b.cmd""
2022-04-13T13:01:23.3655983Z ECHO is off.
2022-04-13T13:01:23.3990627Z ##[section]Finishing: CmdLine

my question is closely releted to this one, but I this one doesnt seem to help me :( How to retrieve value from nested object?

I am trying to loop over an object that i get as a parameter, I use a default value as an example, the values in this object are also objects.

I have a template:

parameters: 
  - name: fruits
    type: object
    default:     
      - Banana:
          name: " acuminata"
          url: "www.banana.com/ acuminata"
      - Apple:
          name: "jonaGold"
          url: "www.apple.com"


steps:

- bash: echo ${{parameters.fruits[0].url}}    



- ${{ each fruit in parameters.fruits }}:
  - script: echo ${{fruit.name}}

Both the first bash and the second bash are not working. Is it possible to use objects in azure pipelines like this?

This is my output for both bash steps:
enter image description here

2022-04-13T13:01:21.8035586Z ##[section]Starting: CmdLine
2022-04-13T13:01:21.8178179Z ==============================================================================
2022-04-13T13:01:21.8178533Z Task         : Command line
2022-04-13T13:01:21.8178874Z Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
2022-04-13T13:01:21.8179171Z Version      : 2.201.1
2022-04-13T13:01:21.8179614Z Author       : Microsoft Corporation
2022-04-13T13:01:21.8179954Z Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
2022-04-13T13:01:21.8180355Z ==============================================================================
2022-04-13T13:01:23.2732262Z Generating script.
2022-04-13T13:01:23.2840010Z Script contents: shell
2022-04-13T13:01:23.2849739Z echo
2022-04-13T13:01:23.3217106Z ========================== Starting Command Output ===========================
2022-04-13T13:01:23.3517504Z ##[command]"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\7a91b1cc-432d-4a20-b39c-7c6b0c78724b.cmd""
2022-04-13T13:01:23.3655983Z ECHO is off.
2022-04-13T13:01:23.3990627Z ##[section]Finishing: CmdLine

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2025-01-28 00:48:12

我刚刚找到了解决方案。另一个线程确实提供了答案,我只需要更好地阅读:P。

您可以使用“value”关键字访问这些值。我还删除了关键字之前的“-”,因此它不再是数组。代码现在看起来像这样:

parameters: 
  - name: fruits
    type: object
    default:     
      Banana:
        name: " acuminata"
        url: "www.banana.com/ acuminata"
      Apple:
        name: "jonaGold"
        url: "www.apple.com"


steps:
   

- ${{ each fruit in parameters.fruits }}:
  - script: echo ${{fruit.value.name}}

我得到这个输出:
输入图片这里的描述

我还没有让第一个工作正常,但我的项目并不真正需要这个

- bash: echo ${{parameters.fruits[0].value.url}}    

I just found a solution. The other thread does provide an answer, I just need to read better :P.

You can access the values with the 'value' keyword. I also removed the '-' before the keywords so its not an array anymore. The code now looks like this:

parameters: 
  - name: fruits
    type: object
    default:     
      Banana:
        name: " acuminata"
        url: "www.banana.com/ acuminata"
      Apple:
        name: "jonaGold"
        url: "www.apple.com"


steps:
   

- ${{ each fruit in parameters.fruits }}:
  - script: echo ${{fruit.value.name}}

And I get this output:
enter image description here

I did not yet get the first one working, but i don't really need this for my project

- bash: echo ${{parameters.fruits[0].value.url}}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文