简体   繁体   English

如何在 React 中彻底映射包含其他对象和数组的整个数组?

[英]How do I map thorough the entire array which contains other objects and arrays as well in React?

I have created a JavaScript array that contains some other objects.我创建了一个包含其他一些对象的 JavaScript 数组。 the subobjects contain arrays.子对象包含数组。 Now I want to iterate through the entire parent object in React.现在我想遍历 React 中的整个父对象。 I use the following approach but it does not work:我使用以下方法但它不起作用:

Array大批

const users =[
    {
        id:1,
        username: "user1",
        path: "/user",
        listItems: {lists:["list-1", "List-2", "List-3", "List-4", "List-5"], path: ["/list-1", "/List-2", "/List-3", "/List-4", "/List-5"]}
    },
    {
        id:2,
        username: "user2",
        path: "/user2",
        listItems: {lists:["list-1", "List-2", "List-3", "List-4", "List-5"], path: ["/list-1", "/List-2", "/List-3", "/List-4", "/List-5"]}
    },
    {
        id:3,
        username: "user3",
        path: "/user3",
        listItems: {lists:["list-1", "List-2", "List-3", "List-4", "List-5"], path: ["/list-1", "/List-2", "/List-3", "/List-4", "/List-5"]}
    }
]

usersInfo component用户信息组件

export default function usersInfo(props){
const users = props.users;
return(
<div className="container">
{users.map((item)=>{
    return(
        <section className="user">
        <h1>{item.username}</h1>
        <p>{item.path}</p>
        <Link href={item.path}>Visit</Link>
        <p>Users lists</p>
        <ul>
        {item.listItems.map((topic)=>{
            <Link href={topic.path}<li>{topic.lists}</li>
        })}
        </ul>
        </section>  
)})}

</div>
)}

Using usersInfo coponent in React:在 React 中使用usersInfo组件:

<usersInfo users = {users}/>

Everything works fine.一切正常。 But I get an issue when trying to map through the listItems object which contains two arrays lists and path .但是当我尝试通过包含两个数组listspathlistItems对象进行映射时遇到问题。 How do I solve this?我该如何解决这个问题?

I expect the output like this:我希望输出是这样的:

在此处输入图片说明

you missing that,你错过了,

replace this, this is wrong .map() wont work here, as listItems its json object, you need change this code,替换这个,这是错误的 .map() 在这里不起作用,因为listItems它的 json 对象,您需要更改此代码,

    <ul>
    {item.listItems.map((topic)=>{
        <Link href={topic.path}<li>{topic.lists}</li>
    })}
    </ul>

replace to this, this will work替换为此,这将起作用

<ul>
{
  item.listItems.lists.map((list)=>{
    <li>{list}</li>
  })
  item.listItems.path.map((topic)=>{
    <Link href={topic.path}<li>{topic.lists}</li>
  })
}
</ul>

If it's guaranteed that lists and paths indices match and you want your <li> elements to contain the links, you can simply do:如果保证listspaths索引匹配并且您希望<li>元素包含链接,则可以简单地执行以下操作:

<ul>
    {item.listItems.lists.map((listItem, listIndex)=>{
            <li><Link href={item.listItems.path[listIndex]}>{listItem}</Link></li>
    })}
</ul>

You are trying to use map method on an Object.您正在尝试在对象上使用 map 方法。 map is a Array method and not available for object. map 是一个数组方法,不适用于对象。 Object.keys.map must solve this. Object.keys.map必须解决这个问题。

export default function usersInfo(props){
const users = props.users;
return(
<div className="container">
{users.map((item)=>{
    return(
        <section className="user">
        <h1>{item.username}</h1>
        <p>{item.path}</p>
        <Link href={item.path}>Visit</Link>
        <p>Users lists</p>
        <ul>
        {Object.keys(item.listItems)[0].map((list, i)=>{
          
            <Link href={item.listItems.path[i]}<li>{list}</li>
        })}
        </ul>
        </section>  
)})}

</div>
)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何 map arrays 的 arrays 数组与反应中的对象 - How to map an array of arrays of arrays with objects in react 将包含 arrays 对象的数组渲染为 React 中的数据表 - Render array which contains arrays of objects as data table in React 如何在 React 中映射一个 prop,它是一个对象数组 - How to map a prop in React, which is an array of objects angular 2-如何遍历包含对象的JSON响应,该对象包含对象数组? - angular 2 - how do I iterate through a JSON response that contains an object which contains an array of objects? 我如何使用 map 打印一个 arrays 数组,数组中的对象反应? - How can I print using map an array of arrays with objects within array react? 我如何通过Lodash拆分包含另一个数组的对象数组 - How do I can split an array of objects that contains another arrays by Lodash 获取对象数组后,如何使用 if 语句有条件地映射多个数组? (反应) - How can I conditionally map multiple arrays with if statements after fetching an array of objects? (React) 如何在包含 object 的反应 js 中的 map 数组? - How to map array in react js which contains object? 我如何 map 一个 JSON API 响应,其中包含到 ag 网格表的对象数组 - How do i map a JSON API response that contains an array of objects to an ag-grid table 如何合并包含对象的数组? - how to merge arrays which contains objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM