Javascript 如何在react中将对象映射到selectsearch的对象数组中?
所以,我想将键和值映射到一个数组中,作为selectsearch选项使用。我尝试了select,它通过单独映射每个项来工作,现在我需要将所有数据映射到一个数组中。我怎么能这么做 正在从api获取数据:Javascript 如何在react中将对象映射到selectsearch的对象数组中?,javascript,reactjs,web,select,frontend,Javascript,Reactjs,Web,Select,Frontend,所以,我想将键和值映射到一个数组中,作为selectsearch选项使用。我尝试了select,它通过单独映射每个项来工作,现在我需要将所有数据映射到一个数组中。我怎么能这么做 正在从api获取数据: export default function Dashboard() { const [value, setValue] = useState(); const [students, setstudents] = useState([]); useEffect(() => {
export default function Dashboard() {
const [value, setValue] = useState();
const [students, setstudents] = useState([]);
useEffect(() => {
const fetchStudents = async () => {
try {
const resp = await Axios({
method: "GET",
url: "https://jsonplaceholder.typicode.com/users",
headers: {
"Content-Type": "application/json",
},
});
setstudents(resp.data);
} catch (err) {}
};
fetchStudents();
}, []);
const classes = useStyles();
const courses=students=>{return students.map(student=>[
<a
href={`http://localhost:3000/teacher/course/:${student.id}`}
>
{student.id}
</a>]);
}// tried to map into an array of links not working
导出默认功能仪表板(){
const[value,setValue]=useState();
const[students,setstudents]=useState([]);
useffect(()=>{
const fetchStudents=async()=>{
试一试{
const resp=等待Axios({
方法:“获取”,
url:“https://jsonplaceholder.typicode.com/users",
标题:{
“内容类型”:“应用程序/json”,
},
});
设置学生(相应数据);
}捕获(错误){}
};
吸引学生();
}, []);
const classes=useStyles();
const courses=students=>{返回students.map(student=>[
]);
}//试图映射到一个链接数组中,但不起作用
使选择选项的下拉框工作正常:
<select
onChange={e => setValue(e.currentTarget.value)}>
{students.map(student =>
<option key={student.id} value={student.username}>
{student.id}
</option>
)}
</select>//worked here
setValue(e.currentTarget.value)}>
{students.map(student=>
{student.id}
)}
//在这里工作
当我使用selectsearch时,我不知道如何将键和值映射到一个数组中,因为selectsearch将选项作为键和值的数组,而不像select:
<SelectSearch
name="form-field-name"
value={this.state.value}
onChange={this.handleChange}
clearable={this.state.clearable}
/>
{students.map(student =>
<options key={student.id} value={student.username}>
{student.id}
</options> )} //couldnt figure out how to make the array of options into single constant
{students.map(student=>
{student.id}
)}//无法理解如何将选项数组设置为单个常量
这应该行得通
const courses=()=>students.map(student=>
<a
href={`http://localhost:3000/teacher/course/:${student.id}`}
>
{student.id}
</a>);
const课程=()=>student.map(student=>
);
另外,如果您不想创建单独的函数,那么
export default function App() {
const students = [];
return (
<div className="App">
{students.map(student=>
<a href={`http://localhost:3000/teacher/course/:${student.id}`}>
{student.id}
</a>)}
</div>
);
}
导出默认函数App(){
康斯特学生=[];
返回(
{students.map(student=>
)}
);
}
您可以添加更多信息吗。。。我有这个结果,但我想。。。。谢谢我想使用select,但搜索栏,所以我尝试selecsearch,但它采取的区别,一次选项不喜欢,,。。。。因此,我需要一种方法将所需的api数据映射到一组选项中,以将其提供给selectsearch。。。感谢您的帮助如果您使用的SelectSearch
错误,这些选项应作为组件的道具提供。那标签在那里干什么?检查