For..in 只返回第一项 - React js
For..in is only returning the first item - React js
我正在遍历一个对象以获得 return 个值。当我 console.log 结果时代码工作正常,但当我尝试将其呈现为文本时代码不起作用。由于某种原因,它只有 return 是第一个值。我做错了什么?
代码如下:
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)
}
渲染(){
return(
{this.testFunc()}
)
}
问题是,在 for
循环的第一次迭代中,函数 returns 结束了。可能的解决方案是:
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>
)
}
我正在遍历一个对象以获得 return 个值。当我 console.log 结果时代码工作正常,但当我尝试将其呈现为文本时代码不起作用。由于某种原因,它只有 return 是第一个值。我做错了什么?
代码如下:
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)
}
渲染(){ return( {this.testFunc()} ) }
问题是,在 for
循环的第一次迭代中,函数 returns 结束了。可能的解决方案是:
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>
)
}