亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過Golang模板中的屬性值獲取結構數組的元素

通過Golang模板中的屬性值獲取結構數組的元素

Go
互換的青春 2022-10-24 09:19:57
我想在 Golang 模板中顯示某個 WooCommerce 產品自定義屬性的值。type Produkt struct {   ...   Attributes []struct {        ID        int      `json:"id"`        Name      string   `json:"name"`        Position  int      `json:"position"`        Visible   bool     `json:"visible"`        Variation bool     `json:"variation"`        Options   []string `json:"options"`   }   ...}實際的 json 對象如下所示:{   ...   "attributes": [   {},   {      "id": 2,      "name": "Hersteller",      "position": 5,      "visible": true,      "variation": false,      "options": [        "Lana Grossa"      ]    },   {}   ],   ... }因此,從這個示例中,我想找到屬性數組的名稱 =“Hersteller”的元素的“選項”數組 (Lana Grossa) 的第一個元素。我試圖調整語法以按索引獲取元素,但無法讓它工作......<input type="text" value="{{ (index (value .Produkt.Attributes.Name eq "Hersteller").Options 0) }}"/><input type="text" value="{{ (index (Name .Produkt.Attributes eq "Hersteller").Options 0) }}"/><input type="text" value="{{ (index (.Produkt.Attributes.Name["Hersteller"]).Options 0) }}"/>非常感謝任何提示
查看完整描述

1 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

使用模板沒有簡單的方法可以做到這一點。你要先找到你需要的入口,然后看它的內容


{{$name := "" }}

{{ range .Product.Attributes }}

{{if eq .Name "Hersteller"}}

   {{$name = (index .Options 0)}}

{{end}}

{{ end }}

<input type="text" value="{{$name}}"/>


查看完整回答
反對 回復 2022-10-24
  • 1 回答
  • 0 關注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號