javascript - For..in 只返回第一项 - React js

标签 javascript for-in-loop

我正在循环一个对象以返回值。当我 console.log 结果时,代码工作正常,但当我尝试将其呈现为文本时,代码不起作用。由于某种原因它只返回第一个值。我做错了什么?

代码如下:

  testFunc = () => {

let spaceship = {
    crew: {
    captain: {
        name: 'Lily',
        degree: 'Computer Engineering',
        cheerTeam() { console.log('You got this!') }
        },
    'chief officer': {
        name: 'Dan',
        degree: 'Aerospace Engineering',
        agree() { console.log('I agree, captain!') }
        },
    medic: {
        name: 'Clementine',
        degree: 'Physics',
        announce() { console.log(`Jets on!`) } },
    translator: {
        name: 'Shauna',
        degree: 'Conservation Science',
        powerFuel() { console.log('The tank is full!') }
        }
    }
};

for(let crewMember in spaceship.crew){
   return spaceship.crew[crewMember].name
}

 }


render(){
  return(
    <div>
     {this.testFunc()}
    </div>
  )
}

新代码,不渲染

  testFunc = () => {

let spaceship = {
    crew: {
    captain: {
        name: 'Lily',
        degree: 'Computer Engineering',
        cheerTeam() { console.log('You got this!') }
        },
    'chief officer': {
        name: 'Dan',
        degree: 'Aerospace Engineering',
        agree() { console.log('I agree, captain!') }
        },
    medic: {
        name: 'Clementine',
        degree: 'Physics',
        announce() { console.log(`Jets on!`) } },
    translator: {
        name: 'Shauna',
        degree: 'Conservation Science',
        powerFuel() { console.log('The tank is full!') }
        }
    }
};

return Object.entries(spaceship.crew).map(member =>  member.name)

}

渲染(){ 返回( {this.testFunc()} ) }

最佳答案

问题是,在 for 循环的第一次迭代中,函数返回并结束。可能的解决方案是:

testFunc = () => {

   let spaceship = {
      crew: {
         captain: {
            name: 'Lily',
            degree: 'Computer Engineering',
            cheerTeam() { console.log('You got this!') }
         },
         'chief officer': {
            name: 'Dan',
            degree: 'Aerospace Engineering',
            agree() { console.log('I agree, captain!') }
         },
         medic: {
            name: 'Clementine',
            degree: 'Physics',
            announce() { console.log(`Jets on!`) } },
            translator: {
               name: 'Shauna',
               degree: 'Conservation Science',
               powerFuel() { console.log('The tank is full!') }
            }
         }
   };
   var names = []
   for(var key in spaceship.crew){
       names.push(spaceship.crew[key].name)
   }

   return names // Return an array with the names
}

然后,在渲染中,您应该执行以下操作:

render(){
   return(
      <div>
         {this.testFunc().map(name => <p> name </p>)}
      </div>
   )
}

关于javascript - For..in 只返回第一项 - React js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53510153/

相关文章:

javascript - 如何使用javascript仅删除字符串中的html标签

javascript - 如何在 kotlin JS 中通过 JSON.stringify 将 Map 序列化为 JSON 字符串?

swift - 常量序列可以使用for-in循环迭代,但不能直接调用next()?

javascript - JavaScript 的 for in 循环是否遍历方法?

swift - 何时使用 forEach(_ :) instead of for in?

javascript - 为什么html标签包含div两次会触发回调?

javascript - 如何管理单击事件以显示/隐藏 React 中的某些元素?

javascript - 使用 mongoDB 和 mongoose 获取附近的地点

javascript - 使用空主体 for-in 循环将对象属性复制到数组

javascript - For..In 循环覆盖所有数组值