admin管理员组

文章数量:1293705

In a WordPress SelectControl, instead of having fixed labels and values for options, how to put labels options out of a parsed json ?

e.g. myjson = {"A1":"1","A2","2} where A1 is the label and 1 is the value

 <SelectControl
       label="Asset id"
       value={this.attributes.asset_id}
       options=[{label: "Select one", value: null, disabled: true},
                {label: "A1", value: "A1id"},
                {label: "A2", value: "A2id"},
                {label: "A3", value: "A3id"},
                {label: "A4", value: "A4id"}]
       onChange={(newval) =>  this.setAttributes({ asset_id: newval })}
                                        />

In a WordPress SelectControl, instead of having fixed labels and values for options, how to put labels options out of a parsed json ?

e.g. myjson = {"A1":"1","A2","2} where A1 is the label and 1 is the value

 <SelectControl
       label="Asset id"
       value={this.attributes.asset_id}
       options=[{label: "Select one", value: null, disabled: true},
                {label: "A1", value: "A1id"},
                {label: "A2", value: "A2id"},
                {label: "A3", value: "A3id"},
                {label: "A4", value: "A4id"}]
       onChange={(newval) =>  this.setAttributes({ asset_id: newval })}
                                        />
Share Improve this question edited May 1, 2021 at 7:20 Dimitrios Ververidis asked Apr 30, 2021 at 15:50 Dimitrios VerveridisDimitrios Ververidis 1426 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Never mind I found it.

let selectEntries = Object.entries(myjson).map( ([k, v]) => ({label: k, value: v}) );

本文标签: javascriptDynamically insert values in a WordPress SelectControl Options field