[英]How to Render this Array in array object
Hi I want to render this Advice on the screen but I could not do it I tried to map but that didn't helped please help me您好,我想在屏幕上显示此建议,但我做不到 我尝试拨打 map 但没有帮助,请帮助我
import React, { useState, useEffect } from 'react' export default function UsersData() { const [Users, setUsers] = useState([{ "slip": { "id": 41, "advice": "Don't use Excel or Powerpoint documents for your basic word processing needs." } }]) return( <> <h2> React Fetch API Example </h2> <ul> {/* Not sure what to write here */} </ul> </> ) }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
I tried {Users.map(e=>{e.slip})
but it didn't work.我尝试
{Users.map(e=>{e.slip})
但它没有用。
It should be as simple as writing a mapping function:它应该像写一个映射 function 一样简单:
export default function UsersData() {
const [Users, setUsers] = useState([
{
slip: {
id: 41,
advice:
"Don't use Excel or Powerpoint documents for your basic word processing needs.",
},
},
]);
return (
<>
<h2>React Fetch API Example</h2>
<ul>
{Users.map((user) => (
<li key={user.slip.id}>{user.slip.advice}</li>
))}
</ul>
</>
);
}
Using map function you can print whole array and sample code below here.使用 map function 您可以在此处打印整个数组和示例代码。
<ul>
{Users.map((element) => (
<li key={element.slip.id}>{element.slip.advice}</li>
))}
</ul>
Use this:用这个:
import React, { useState, useEffect } from 'react'
export default function UsersData() {
const [Users, setUsers] = useState([
{
"slip": {
"id": 41,
"advice": "Don't use Excel or Powerpoint documents for your basic word processing needs."
}
}
])
return (
<>
<h2>React Fetch API Example</h2>
<ul>
{Users.map(({slip})=>(
<li key={slip.id}>{slip.advice}</li>
))}
</ul>
</>
)
}
<h2>React Fetch API Example</h2>
<ul>
{Users.map((user) =>
Object.keys(user).map((key) => (
<li>
{user[key].id} - {user[key].advice}
</li>
))
)}
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.