Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript如何从映射数组中获取长度_Javascript_Reactjs_Jsx - Fatal编程技术网

JavaScript如何从映射数组中获取长度

JavaScript如何从映射数组中获取长度,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,我是JSX新手,我正在尝试获取每个团队的员工人数this.state.teams[0].employee.length将为数组中的[0]对象提供员工人数,如何获取每个团队的员工人数并只渲染一次 render() { const {teams, isLoading} = this.state; const { location } = this.props; if (isLoading) { return <p>Loading...</p

我是JSX新手,我正在尝试获取每个团队的员工人数
this.state.teams[0].employee.length
将为数组中的
[0]
对象提供员工人数,如何获取每个团队的员工人数并只渲染一次

render() {
    const {teams, isLoading} = this.state;
    const { location } = this.props;

    if (isLoading) {
        return <p>Loading...</p>;
    }
    // console.log("Number of Employees team", this.state.teams[0].employee.length);
    

    const teamList = teams.map(team => {

        console.log("TEAM Employees", teams[0].employee.length);

        return <tr key={team.id}>
               <td>
                <div className="tableContent">
                    <div>
                         {team.name} 
                    </div>
                    <div>
                      <ul>{team.employee.map(employee => {
                       return <li key={employee.id}>{employee.name}: {employee.email}</li>
                      })}</ul>
                    </div>
                    <div>
                     {team.employee.map(employee => {
                       return <li key={employee.id}>{teams[0].employee.length}</li>
                      })}
                    </div>
                </div>                    
            </td>
        </tr>
    });
render(){
const{teams,isLoading}=this.state;
const{location}=this.props;
如果(孤岛加载){
返回加载…

; } //console.log(“团队的员工数量”,this.state.teams[0].employee.length); const teamList=teams.map(team=>{ console.log(“团队员工”,团队[0].employee.length); 返回 {team.name}
    {team.employee.map(employee=>{ return
  • {employee.name}:{employee.email}
  • })}
{team.employee.map(employee=>{ return
  • {teams[0].employee.length}
  • })} });
    您可以为特定团队尝试以下方法:

    const teamList = teams.map(team => {
       console.log("TEAM Employees", team.employee.length);
    }
    
    对于所有团队中的员工数量:

    const numberOfAllEmployees = teams.map(team => team.employee.length).reduce((a,b) => a + b);
    

    您可以使用
    Object.key(团队[0].employee).length

    方法返回给定对象自身的数组 可枚举属性名,按与普通属性相同的顺序迭代 我会的


    什么是
    teams
    teams
    constructor
    this.state={teams:[]}中设置的数组,带有一个api响应,该api响应
    setState({teams:data})
    您能给出
    teams
    数组中对象的示例吗这是
    控制台.log(teams)
    0:{id:1 name:“Team1”,employees:Array(2)}