简体   繁体   English

如何有条件地渲染映射数组中的映射对象

[英]How conditionally render mapped object in mapped array

I have two different pieces of data coming into my component, sometimes an array of objects is passed in, and sometimes just an object. 我有两个不同的数据进入我的组件,有时会传入一个对象数组,有时只是一个对象。 My goal is to loop through each object and spit out some JSX. 我的目标是遍历每个对象并吐出一些JSX。 Here is my code: 这是我的代码:

                (Array.isArray(tableData))
                    ?
                        (tableData.map(obj => {
                            (Object.keys(obj).map(key => {
                                return (
                                    <tr>
                                        <td>{key}</td>
                                        <td>{obj[key]}</td>
                                    </tr>
                                );
                            }))
                        }))
                    :
                        (Object.keys(tableData).map(key => {
                            return (
                                <tr key={key}>
                                    <td>{key}</td>
                                    <td>{tableData[key]}</td>
                                </tr>
                            );
                        }))

You can see im checking to see if the data coming in is an array, and if not loop through just a regular object. 您可以看到即时消息正在检查以查看传入的数据是否为数组,如果不是,则仅通过常规对象循环。 That part works fine, but if the data is an array, nothing gets displayed. 该部分工作正常,但是如果数据是数组,则不会显示任何内容。 What is wrong with my code that react doesnt render anything or throw any error messages? 我的代码没有反应或呈现任何错误消息,这是怎么回事?

Because you forgot to use return in this line: 因为您忘记在此行中使用return

(Object.keys(obj).map , try this: (Object.keys(obj).map ,试试这个:

Array.isArray(tableData))
    ?
        tableData.map(obj => {
            return Object.keys(obj).map(key => {
                return (
                    <tr>
                        <td>{key}</td>
                        <td>{obj[key]}</td>
                    </tr>
                );
            })
        })
    :
        Object.keys(tableData).map(key => {
            return (
                <tr key={key}>
                    <td>{key}</td>
                    <td>{tableData[key]}</td>
                </tr>
            );
        })

Assign the unique key to element otherwise you will get a warning. 为元素分配唯一键,否则将收到警告。

Mayank's answer solves the problem, but it's a bit verbose. Mayank的答案解决了这个问题,但这有点冗长。 Recall that if you want to return the result of a single expression (eg the result of a function call or a JSX element) from an arrow function, you can omit both the curly braces and return : 回想一下,如果您想从箭头函数返回单个表达式的结果(例如,函数调用或JSX元素的结果),则可以省略花括号并return

Array.isArray(tableData)
  ? tableData.map(obj =>
      Object.keys(obj).map(key => (
        <tr>
          <td>{key}</td>
          <td>{obj[key]}</td>
        </tr>
      )
    ))
  : Object.keys(tableData).map(key => (
      <tr key={key}>
        <td>{key}</td>
        <td>{tableData[key]}</td>
      </tr>
    ))

I've used parentheses above just for clarity. 为了清楚起见,我在上面使用了括号。

However, you're repeating the same code here twice, so for simplicity and readability I suggest extracting it into a function of its own: 但是,您在这里重复了两次相同的代码,因此,为简单起见,我建议将其提取为自己的函数:

const tableRows = obj =>
  Object.keys(obj).map(key => (
    <tr>
      <td>{key}</td>
      <td>{obj[key]}</td>
    </tr>
  )
);

// ...

Array.isArray(tableData) ? tableData.map(tableRows) : tableRows(tableData)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM