Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

javascript - 如何从API响应动态填充选择框(How to populate Select box dynamically from API response)

I'm trying to create select box dynamically from api response in react.(我试图从反应中的api响应动态创建选择框。)

below is the api response(以下是api响应) { "land": [{ "land_id": 15, "name": "zipku", "description": "zipku" }, { "land_id": 16, "name": "jumba", "description": "jumba" }], "type": [{ "type_id": 1, "name": "s_id", "description": "s_id" }, { "type_id": 2, "name": "s_id2", "description": "s_id2" }], "nationality": [{ "id": 1, "name": "Japan", "description": "Japan" }, { "id": 2, "name": "Hong_Kong", "description": "Hong_Kong" }] } I am using react with ant design.(我正在使用与蚂蚁设计反应。) what I have did so far(到目前为止我所做的) Object.keys(myObject).map((idx, i) => { return (<Select key={i} {...myObject[idx].map((selec, i) => ( <Option value={selec.name}>{selec.name}}</Option> ))} > </Select>) }) Can someone please explain me what I am doing wrong?(有人可以解释一下我在做什么错吗?)   ask by ConAra translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this one(试试这个)

Object.keys(myObject).map((idx, i) => { return (<Select key={i}> {myObject[idx].map((selec, i) => ( <Option value={selec.name}>{selec.name}</Option> ))} </Select>) })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...