[英]Unable to get property 'map' of nested array in React.js
I'm having an issue mapping an array in React.js. 我在React.js中映射数组时遇到问题。 Below is the snippet of code I'm working on:
以下是我正在处理的代码片段:
var MoveListItemComponent = React.createClass({
render: function () {
return (
<div className="move_list_item">
<p className="character_list_item_country">{this.props.move.move_name}</p>
</div>
);
}
});
var MoveListComponent = React.createClass({
render: function(){
var moves = this.props.moveset.map(function(move){
return(
<MoveListItemComponent key={move.move_name} move={move} />
);
});
return (
<div>
{moves}
</div>
);
}
});
var CharacterPage = React.createClass({
getInitialState: function() {
return {character: {}};
},
componentDidMount: function() {
this.props.service.findById(this.props.characterId).done(function(result) {
this.setState({character: result});
}.bind(this));
},
render: function () {
return (
<div>
<HeaderComponent text="Character Details"/>
<div>
<div className="charcater_page_top_info">
<img className="character_page_icon" src={this.state.character.icon} />
<div className="character_page_info">
<h3 className="character_page_name">{this.state.character.name}</h3>
<p className="character_page_country">{this.state.character.country}</p>
</div>
</div>
<p className="character_page_tagline"><i>{this.state.character.tagline}</i></p>
<div className="character_page_stats">
</div>
<div>
<h2>Special Moves</h2>
<p>{this.state.character.tagline}</p>
<MoveListComponent moveset={this.state.character.special_moves} />
</div>
</div>
</div>
);
}
});
And the data I'm accessing is: 我正在访问的数据是:
characters = [
{"id": 1, "name": 'Ryu', "country": 'Japan', "tagline":"You must defeat Sheng Long to stand a chance.", "stats":{"power": 7, "speed": 7, "jump": 7, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/3/37/Portrait_SF2_Ryu.png', "character_list_item_backgroundcolor": "#ff9b9b"},
{"id": 2, "name": 'E. Honda', "country": 'Japan', "tagline":"Can't you do better than that?", "stats":{"power": 9, "speed": 6, "jump": 6, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/5/5a/Portrait_SF2_EHonda.png', "character_list_item_backgroundcolor": "#9bb9ff"},
{"id": 3, "name": 'Blanka', "country": 'Brazil', "tagline":"Seeing you in action is a joke.", "stats":{"power": 7, "speed": 6, "jump": 6, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/a/ad/Portrait_SF2_Blanka.png', "character_list_item_backgroundcolor": "#a4cc9b"},
{"id": 4, "name": 'Guile', "country": 'USA', "tagline":"Go home and be a family man.", "stats":{"power": 8, "speed": 8, "jump": 7, "range": 8}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/4/4e/Portrait_SF2_Guile.png', "character_list_item_backgroundcolor": "#f3ff8e"},
{"id": 5, "name": 'Ken', "country": 'USA', "tagline":"Attack me if you dare, I will crush you.", "stats":{"power": 7, "speed": 7, "jump": 7, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/5/50/Portrait_SF2_Ken.png', "character_list_item_backgroundcolor": "#ff8466"},
{"id": 6, "name": 'Chun Li', "country": 'China', "tagline":"I'm the strongest woman in the world.", "stats":{"power": 6, "speed": 9, "jump": 9, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/e/e2/Portrait_SF2_ChunLi.png', "character_list_item_backgroundcolor": "#6670ff"},
{"id": 7, "name": 'Zangief', "country": 'USSR', "tagline":"My strength is much greater than yours.", "stats":{"power": 7, "speed": 5, "jump": 4, "range": 4}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/8/81/Portrait_SF2_Zangief.png', "character_list_item_backgroundcolor": "#ffa551"},
{"id": 8, "name": 'Dhalsim', "country": 'India', "tagline":"I will meditate and then destroy you.", "stats":{"power": 5, "speed": 4, "jump": 6, "range": 10}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/8/88/Portrait_SF2_Dhalsim.png', "character_list_item_backgroundcolor": "#ffea51"}
];
So everything renders nicely except the MoveListComponent. 因此,除了MoveListComponent之外,其他所有内容都可以很好地呈现。 I'm pretty sure the issue is how it is mapped in the MoveListComponent class, I just don't know exactly what is wrong.
我很确定问题是在MoveListComponent类中它是如何映射的,我只是不知道到底是什么问题。 I was using the same method to render a list of the all the characters, and that worked out well for me.
我使用相同的方法来呈现所有字符的列表,这对我来说效果很好。 Am I missing a reference to the list of special moves?
我是否缺少对特殊举动列表的引用?
My full code can be seen here https://github.com/ChoragosDesigns/ChoragosDesigns.github.io . 我的完整代码可以在这里https://github.com/ChoragosDesigns/ChoragosDesigns.github.io看到。 Thank you in advance.
先感谢您。
Issue is in this line: 问题在这一行:
<MoveListComponent moveset={this.state.character.special_moves} />
character is an array
of object
, so to access the special_moves
of any object
, you need to specify the index also, use this: character是
object
的array
,因此要访问任何object
的special_moves
,还需要指定索引,请使用以下命令:
<MoveListComponent moveset={this.state.character[0].special_moves} />
Or use map
on this, like this: 或像这样使用
map
:
{
this.state.character.map( (item, i) => {
return <MoveListComponent moveset={item.special_moves} />
})
}
Check the character array: 检查字符数组:
character = [
{...
"special_moves": [
{"move_name":"amove", "thesteps":"left, left, up"},
{"move_name":"asecondmove", "thesteps":"left, right, punch"}
],
},{...}
]
Check the working code: 检查工作代码:
var characters = [ {"id": 1, "name": 'Ryu', "country": 'Japan', "tagline":"You must defeat Sheng Long to stand a chance.", "stats":{"power": 7, "speed": 7, "jump": 7, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/3/37/Portrait_SF2_Ryu.png', "character_list_item_backgroundcolor": "#ff9b9b"}, {"id": 2, "name": 'E. Honda', "country": 'Japan', "tagline":"Can't you do better than that?", "stats":{"power": 9, "speed": 6, "jump": 6, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/5/5a/Portrait_SF2_EHonda.png', "character_list_item_backgroundcolor": "#9bb9ff"}, {"id": 3, "name": 'Blanka', "country": 'Brazil', "tagline":"Seeing you in action is a joke.", "stats":{"power": 7, "speed": 6, "jump": 6, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/a/ad/Portrait_SF2_Blanka.png', "character_list_item_backgroundcolor": "#a4cc9b"}, {"id": 4, "name": 'Guile', "country": 'USA', "tagline":"Go home and be a family man.", "stats":{"power": 8, "speed": 8, "jump": 7, "range": 8}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/4/4e/Portrait_SF2_Guile.png', "character_list_item_backgroundcolor": "#f3ff8e"}, {"id": 5, "name": 'Ken', "country": 'USA', "tagline":"Attack me if you dare, I will crush you.", "stats":{"power": 7, "speed": 7, "jump": 7, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/5/50/Portrait_SF2_Ken.png', "character_list_item_backgroundcolor": "#ff8466"}, {"id": 6, "name": 'Chun Li', "country": 'China', "tagline":"I'm the strongest woman in the world.", "stats":{"power": 6, "speed": 9, "jump": 9, "range": 7}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/e/e2/Portrait_SF2_ChunLi.png', "character_list_item_backgroundcolor": "#6670ff"}, {"id": 7, "name": 'Zangief', "country": 'USSR', "tagline":"My strength is much greater than yours.", "stats":{"power": 7, "speed": 5, "jump": 4, "range": 4}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/8/81/Portrait_SF2_Zangief.png', "character_list_item_backgroundcolor": "#ffa551"}, {"id": 8, "name": 'Dhalsim', "country": 'India', "tagline":"I will meditate and then destroy you.", "stats":{"power": 5, "speed": 4, "jump": 6, "range": 10}, "special_moves":[{"move_name":"amove", "thesteps":"left, left, up"}, {"move_name":"asecondmove", "thesteps":"left, right, punch"}], "icon":'https://cdn.wikimg.net/strategywiki/images/8/88/Portrait_SF2_Dhalsim.png', "character_list_item_backgroundcolor": "#ffea51"} ]; var MoveListItemComponent = React.createClass({ render: function () { return ( <div className="move_list_item"> <p className="character_list_item_country">{this.props.move.move_name}</p> </div> ); } }); var MoveListComponent = React.createClass({ render: function(){ var moves = this.props.moveset.map(function(move){ return( <MoveListItemComponent key={move.move_name} move={move} /> ); }); return ( <div> {moves} </div> ); } }); var CharacterPage = React.createClass({ getInitialState: function() { return {character: characters}; }, render: function () { return ( <div> <h2>Special Moves</h2> <p>{this.state.character[0].tagline}</p> { this.state.character.map( (item, i) => { return <MoveListComponent moveset={item.special_moves} /> }) } </div> ); } }); ReactDOM.render(<CharacterPage/>, document.getElementById('app'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id='app'/>
So I focused on a comment @elmeister made about the list of special moves not being made yet. 因此,我重点关注@elmeister对尚未采取的特殊措施清单的评论。 So I decided to keep how I was mapping pretty much the same, and then in the rendering of the final component, I put the SpecialMovesListComponent inside a variable.
因此,我决定保持映射的方式几乎相同,然后在渲染最终组件时,将SpecialMovesListComponent放入变量中。
var SpecialMoveComponent = React.createClass({
render: function(){
return(
<div>
<h3 className="character_page_move_name">{this.props.move.move_name}</h3>
<p className="character_page_move_steps"><i>{this.props.move.move_steps}</i></p>
</div>
);
}
});
var SpecialMoveListComponent = React.createClass({
render: function(){
var special_moves = this.props.moves.map(function(move){
return(
<SpecialMoveComponent move={move} />
);
});
return(
<div className="special_move_list">
{special_moves}
</div>
);
}
});
var CharacterPage = React.createClass({
getInitialState: function() {
return {character: {}};
},
componentDidMount: function() {
this.props.service.findById(this.props.characterId).done(function(result) {
this.setState({character: result});
}.bind(this));
},
render: function () {
var special_move_list_component = "";
//---When this component is mounted (or rendered) the special_moves isn't quite ready to exist. So first
// I check if it is ready, then render the component that handles the mapping, and put that in a variable
// which is called in this component's return
if(this.state.character.special_moves)
{
special_move_list_component = <SpecialMoveListComponent moves={this.state.character.special_moves} />;
}
return (
<div>
<HeaderComponent text="Character Details"/>
<div>
<div className="charcater_page_top_info">
<img className="character_page_icon" src={this.state.character.icon} />
<div className="character_page_info">
<h3 className="character_page_name">{this.state.character.name}</h3>
<p className="character_page_country">{this.state.character.country}</p>
</div>
</div>
<p className="character_page_tagline"><i>{this.state.character.tagline}</i></p>
<h2 className="section_title">Special Moves</h2>
{special_move_list_component}
</div>
</div>
);
}
});
Thank you everyone for your help, and hopefully this answer is clear enough for others. 谢谢大家的帮助,希望这个答案对其他人足够清楚。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.