如何循环显示使数组值复杂化:React.js

时间:2019-07-17 03:13:06

标签: javascript arrays reactjs arraylist sub-array

我将为返回状态的显示数组值创建循环。现在,我按{text.nameSub [0] .nameSub [0] [0] .nameSub},{text.nameSub [0] .nameSub [0] [1] .nameSub}显示,但我不知道create for循环而是这段代码。请帮助我,非常感谢您的建议。

import React,{Component} from 'react'
import '../App.css'

class addEntity extends Component {

    constructor(props){
        super(props);
        this.state ={
            index: 1,
            entity: []
        }
    }
    render() {
        return(
            <div>
                <table class="table">
                    <thead>
                        <tr>
                            <th scope="col">#</th>
                            <th scope="col">Entity Type</th>
                            <th scope="col">Sub-type</th>
                            <th scope="col">Actions</th>
                        </tr>
                    </thead>
                        {this.state.entity.map((text,index) =>{
                            return(
                                <tr key={text.nameEn}>
                                    <th>{index+1}</th>
                                    <td scope="col">{text.nameEn}</td>
                                    <td scope="col">
                                    {text.nameSub[0].nameSub[0][0].nameSub},
                                    {text.nameSub[0].nameSub[0][1].nameSub}
{/* I will create loop for display it here like this {text.nameSub[0].nameSub[0][i].nameSub} */}
                                    </td>
                                </tr>
                            )
                        })}
                </table>

            </div>
        )
    }
}

export default addEntity;

1 个答案:

答案 0 :(得分:0)

您可以这样迭代

{this.state.entity.map((text,index) =>{
     return(
        <tr key={text.nameEn}>
            <th>{index+1}</th> //this might be `td` and not `th`
            <td scope="col">{text.nameEn}</td>
            <td scope="col">
              {text.nameSub[index].map((name,ind) => {
                 return <span>{name[ind].nameSub}</span> 
              })}
            </td>
        </tr>
     )
})}