2017-05-25 35 views
0

我有一个JSON数组这样.MAP功能不工作

"Nodes": [ 
{ 
    "Name": "abc", 
    "ID": "123", 
    "type": "xyz", 
    "connections": [ 
    { 
     "ipAddress": "1.1.2.2", 
     "username": "name", 
     "type": "mno" 
    }, 
    { 
     "ipAddress": "1.1.2.3", 
     "username": "name2", 
     "type": "mno2" 
    } 
    ] 
}, 
{ 
    "Name": "abc2", 
    "ID": "124", 
    "type": "xyz2", 
    "connections": [ 
    { 
     "ipAddress": "1.1.2.4", 
     "username": "name3", 
     "type": "mno3" 
    } 
    ] 
} ] 

想显示,作为一个表以用于每个连接设置的密码。我的代码如下所示。

_passwordManagement: function() { 

     return (
      <Table> 
       <thead> 
        <tr> 
         <th> 
          IP Address 
         </th> 
         <th> 
          User Name 
         </th> 
         <th> 
          Password 
         </th> 
         <th> 
          Re-Enter Password 
         </th> 
         <th> 
         </th> 
        </tr> 
       </thead> 
      <tbody> 
      {this.state.all.map(function(nodes, i) { 
       if (nodes.connections.length == 0){ 
        console.log("This has no node"); 
       } else if (nodes.connections.length > 1) { 
        {nodes.connections.map(function(conn) { 
         return(
         <TableRow> 
          <td> 
           {conn.ip} 
          </td> 
          <td> 
           {conn.username} 
          </td> 
          <td className='secondary'> 
           <TextInput type="password" placeHolder="*****" className="passwordInput"/> 
          </td> 
          <td className='secondary'> 
           <TextInput type="password" placeHolder="*****" className="passwordInput"/> 
          </td> 
          <td className='secondary'> 
           <Button label='Set Password' className="tableButton" /> 
          </td> 
         </TableRow> 
         ); 
        }, this) 
        } 

       } else if (nodes.connections.length == 1) { 
        return(
         <TableRow key={i}> 
          <td> 
           {nodes.connections[0].ip} 
          </td> 
          <td> 
           {nodes.connections[0].username} 
          </td> 
          <td className='secondary'> 
           <TextInput type="password" placeHolder="*****" className="passwordInput"/> 
          </td> 
          <td className='secondary'> 
           <TextInput type="password" placeHolder="*****" className="passwordInput"/> 
          </td> 
          <td className='secondary'> 
           <Button label='Set Password' className="tableButton" /> 
          </td> 
         </TableRow> 
        ); 
       } 

      }, this)} 
      </tbody> 
     </Table> 
     ) 
    } 

这里如果条件> 1连接没有返回任何东西。第三个条件== 1正在返回行。

帮助我理解我在做什么错。提前致谢。

+0

您没有对阵列节点[i] .connections.length> 1 –

+0

的索引已经在外侧使用map函数,因此不需要节点[i],因为它在任何时候都只有一个项目。 –

+0

'this.state.all'代表什么? –

回答

0

在第二种情况下,您不会返回任何外部地图功能。只返回内部地图功能。第三种情况有回报,这就是为什么它的工作。