React 中的地图过滤器。对象作为 React 子项无效。如果您打算呈现子集合,请改用数组
Map Filter in React. Objects are not valid as a React child. If you meant to render a collection of children, use an array instead
function Main(){
// create an array that holds objects
const people = [
{
firstName: 'Fathi',
lastName: 'Noor',
age: 27,
colors: ['red', 'blue'],
bodyAttributes: {
weight: 64,
height: 171
}
},
{
firstName: 'Hanafi',
lastName: 'Noor',
age: 26,
colors: ['white', 'black'],
bodyAttributes: {
weight: 62,
height: 172
}
}
]
return(
<main>
<div>
/* display person age equal to 27 using filter using ES6 arrow */
{people.filter(person => person.age == 27)}
</div>
</main>
)
}
我对 react 和 map 的使用非常陌生,而不是普通的循环函数。
当我 运行 这段代码时,它给了我这个错误(如下所示)
你们能教我当按年龄过滤等于27时如何显示一个人的所有数据的正确方法吗?
您尝试 return 对象,但这是不可能的。
function Main(){
// create an array that holds objects
const people = [
{
firstName: 'Fathi',
lastName: 'Noor',
age: 27,
colors: ['red', 'blue'],
bodyAttributes: {
weight: 64,
height: 171
}
},
{
firstName: 'Hanafi',
lastName: 'Noor',
age: 26,
colors: ['white', 'black'],
bodyAttributes: {
weight: 62,
height: 172
}
}
]
const filterdPeople = people.filter(person => person.age == 27)
return(
<main>
<div>
{filterdPeople.map(person => {
return (
<div key={person.lastName}>
<p>First name: {person.firstName}</p>
<p>Last name: {person.lastName}</p>
<p>Age: {person.age}</p>
<p>Colors: {person.colors.map(color => (<span>{`${color}, `}</span>))}</p>
<p>
<span>Body Attributes: </span>
<span>Weight: {person.bodyAttributes.weight}</span>
<span>Height: {person.bodyAttributes.height}</span>
</p>
</div>
)
})}
</div>
</main>
)
}
你必须map
数组。看到这个 doc.
对于每个人,您决定如何显示每个字段,这里我使用了 <p>
标签,您可以将它们显示在 table 中,也可以显示在自定义卡片中..您的选择!
请记住,{}
括号内是 Javascript 表达式,而 ()
是 JSX,非常类似于 HTML
标签,如您所见!
阅读文档,遵循教程,会有很大帮助!
{people.filter(person => person.age == 27).map(person => { return(
<div>
<p>First name: {person.firstName}</p>
<p>Last name: {person.lastName}</p>
<p>Age: {person.age}</p>
<p>Colors: {person.colors.map(color => (<span>{color+" "}</span>)}</p>
<p>
<span>Body Attributes: </span>
<span>Weight: {person.bodyAttributes.weight}</span>
<span>Height: {person.bodyAttributes.height}</span>
</p>
</div>
)})}
function Main(){
// create an array that holds objects
const people = [
{
firstName: 'Fathi',
lastName: 'Noor',
age: 27,
colors: ['red', 'blue'],
bodyAttributes: {
weight: 64,
height: 171
}
},
{
firstName: 'Hanafi',
lastName: 'Noor',
age: 26,
colors: ['white', 'black'],
bodyAttributes: {
weight: 62,
height: 172
}
}
]
return(
<main>
<div>
/* display person age equal to 27 using filter using ES6 arrow */
<table>
<tr>
<th>firstName</th>
<th>lastName</th>
<th>age</th>
<th>colors</th>
<th>bodyAttributes<th>
</tr>
{people.filter(person => person.age == 27).map(item=>{
return(
<tr>
<td>{item.firstName}</td>
<td>{item.lastName}</td>
<td>{item.age}</td>
<td>
<ul>
{
item.colors.map(color=>{
return <li>{color}</li>
})
}
</ul>
{}
</td>
<td>
<ul>
<li>W:{item.bodyAttributes.weight}</li>
<li>H:{item.bodyAttributes.height}</li>
</ul>
</td>
</tr>
)
})}
</table>
</div>
</main>
)
}
function Main(){
// create an array that holds objects
const people = [
{
firstName: 'Fathi',
lastName: 'Noor',
age: 27,
colors: ['red', 'blue'],
bodyAttributes: {
weight: 64,
height: 171
}
},
{
firstName: 'Hanafi',
lastName: 'Noor',
age: 26,
colors: ['white', 'black'],
bodyAttributes: {
weight: 62,
height: 172
}
}
]
return(
<main>
<div>
/* display person age equal to 27 using filter using ES6 arrow */
{people.filter(person => person.age == 27)}
</div>
</main>
)
}
我对 react 和 map 的使用非常陌生,而不是普通的循环函数。
当我 运行 这段代码时,它给了我这个错误(如下所示)
你们能教我当按年龄过滤等于27时如何显示一个人的所有数据的正确方法吗?
您尝试 return 对象,但这是不可能的。
function Main(){
// create an array that holds objects
const people = [
{
firstName: 'Fathi',
lastName: 'Noor',
age: 27,
colors: ['red', 'blue'],
bodyAttributes: {
weight: 64,
height: 171
}
},
{
firstName: 'Hanafi',
lastName: 'Noor',
age: 26,
colors: ['white', 'black'],
bodyAttributes: {
weight: 62,
height: 172
}
}
]
const filterdPeople = people.filter(person => person.age == 27)
return(
<main>
<div>
{filterdPeople.map(person => {
return (
<div key={person.lastName}>
<p>First name: {person.firstName}</p>
<p>Last name: {person.lastName}</p>
<p>Age: {person.age}</p>
<p>Colors: {person.colors.map(color => (<span>{`${color}, `}</span>))}</p>
<p>
<span>Body Attributes: </span>
<span>Weight: {person.bodyAttributes.weight}</span>
<span>Height: {person.bodyAttributes.height}</span>
</p>
</div>
)
})}
</div>
</main>
)
}
你必须map
数组。看到这个 doc.
对于每个人,您决定如何显示每个字段,这里我使用了 <p>
标签,您可以将它们显示在 table 中,也可以显示在自定义卡片中..您的选择!
请记住,{}
括号内是 Javascript 表达式,而 ()
是 JSX,非常类似于 HTML
标签,如您所见!
阅读文档,遵循教程,会有很大帮助!
{people.filter(person => person.age == 27).map(person => { return(
<div>
<p>First name: {person.firstName}</p>
<p>Last name: {person.lastName}</p>
<p>Age: {person.age}</p>
<p>Colors: {person.colors.map(color => (<span>{color+" "}</span>)}</p>
<p>
<span>Body Attributes: </span>
<span>Weight: {person.bodyAttributes.weight}</span>
<span>Height: {person.bodyAttributes.height}</span>
</p>
</div>
)})}
function Main(){
// create an array that holds objects
const people = [
{
firstName: 'Fathi',
lastName: 'Noor',
age: 27,
colors: ['red', 'blue'],
bodyAttributes: {
weight: 64,
height: 171
}
},
{
firstName: 'Hanafi',
lastName: 'Noor',
age: 26,
colors: ['white', 'black'],
bodyAttributes: {
weight: 62,
height: 172
}
}
]
return(
<main>
<div>
/* display person age equal to 27 using filter using ES6 arrow */
<table>
<tr>
<th>firstName</th>
<th>lastName</th>
<th>age</th>
<th>colors</th>
<th>bodyAttributes<th>
</tr>
{people.filter(person => person.age == 27).map(item=>{
return(
<tr>
<td>{item.firstName}</td>
<td>{item.lastName}</td>
<td>{item.age}</td>
<td>
<ul>
{
item.colors.map(color=>{
return <li>{color}</li>
})
}
</ul>
{}
</td>
<td>
<ul>
<li>W:{item.bodyAttributes.weight}</li>
<li>H:{item.bodyAttributes.height}</li>
</ul>
</td>
</tr>
)
})}
</table>
</div>
</main>
)
}