Javascript 无法使用.map()渲染数组值
我有这个:Javascript 无法使用.map()渲染数组值,javascript,reactjs,Javascript,Reactjs,我有这个: var Astronomy = React.createClass({ getDefaultProps: function() { return {meteo : JSON.parse(localStorage.getItem('meteo')).data}; }, render: function() { return ( <div className="temps"> {this.props.meteo.weather.map(func
var Astronomy = React.createClass({
getDefaultProps: function() {
return {meteo : JSON.parse(localStorage.getItem('meteo')).data};
},
render: function() {
return (
<div className="temps">
{this.props.meteo.weather.map(function(d, i) {return
<div className="waqt">
<div className="temps">
<div className="raise">
<div className="sunraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunrise"]}</i></div>
<div className="sunset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunset"]}</i></div>
</div>
<div className="set">
<div className="moonraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonrise"]}</i></div>
<div className="moonset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonset"]}</i></div>
</div>
</div>
</div>
}
)}
</div>
);
},
componentDidMount: function() {
return console.log(this.props.meteo.weather[0]["astronomy"][0]["sunrise"]);
},
});
var Astronomy=React.createClass({
getDefaultProps:function(){
返回{meteo:JSON.parse(localStorage.getItem('meteo')).data};
},
render:function(){
返回(
{this.props.meteo.weather.map(函数(d,i){return
{this.props.meteo.weather[i][“天文学”][0][“日出”}
{this.props.meteo.weather[i][“天文学”][0][“日落”}
{this.props.meteo.weather[i][“天文学”][0][“月出”}
{this.props.meteo.weather[i][“天文学”][0][“卫星集”]}
}
)}
);
},
componentDidMount:function(){
返回console.log(this.props.meteo.weather[0][“天文学”][0][“日出”]);
},
});
但是我得到了一个空的结果!即使控制台提供了我所期望的06:19 AM
,并使用chrome扩展对其进行调试,我也看到阵列保持了屏幕截图中的样子:
JavaScript将在
return
之后插入分号,如果它后面有换行符。即
function foo() {
return
42
}
与
function foo() {
return;
42
}
i、 e.最后一行将永远不会被计算,并且未定义的
将被返回
返回值必须始终与return
语句在同一行或从同一行开始:
return (
<div>...</div>
);
返回(
...
);
此外,无需通过
this.props.meteo.weather[i]
访问数据。该值已作为d
传递给回调,因此您只需执行d.astronomy[0].sunrise
。在。var Astronomy=React.createClass中了解有关.map
的更多信息({
getDefaultProps:function(){
返回{meteo:JSON.parse(localStorage.getItem('meteo')).data};
},
render:function(){
返回(
{this.props.meteo.weather.map(函数(d,i)){
返回
{this.props.meteo.weather[i][“天文学”][0][“日出”}
{this.props.meteo.weather[i][“天文学”][0][“日落”}
{this.props.meteo.weather[i][“天文学”][0][“月出”}
{this.props.meteo.weather[i][“天文学”][0][“卫星集”]}
},这个)}
);
},
componentDidMount:function(){
返回console.log(this.props.meteo.weather[0][“天文学”][0][“日出”]);
},
});
此
在映射
函数中有更改,您可以通过第二个参数指定它,或使用()=>
ES6箭头函数。wonerful!不仅有效,我还上了一门关于地图
和邪恶分号
的精彩课程,谢谢!但是为什么它即使没有这个也能工作呢?编辑:添加此
,使其进入一个inifinte循环,使浏览器(Chrome)崩溃,因为我没有完整的代码,我无法在您的情况下运行它。这是我的演示,你能编辑答案并解释它的作用吗?因为它不清楚,因为它在没有的情况下工作,但在本例中,当我删除它时,它会被this.props.meteo.weather
调用,因此,在函数中,this
是this.props.meteo.weather
啊,因此,使用d
作为参数做了同样的技巧,再次感谢!如果有人来过这里,如果您不使用钥匙,请添加此
var Astronomy = React.createClass({
getDefaultProps: function() {
return {meteo : JSON.parse(localStorage.getItem('meteo')).data};
},
render: function() {
return (
<div className="temps">
{this.props.meteo.weather.map(function(d, i) {
return <div className="waqt">
<div className="temps">
<div className="raise">
<div className="sunraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunrise"]}</i></div>
<div className="sunset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["sunset"]}</i></div>
</div>
<div className="set">
<div className="moonraise"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonrise"]}</i></div>
<div className="moonset"><i className="riz">{this.props.meteo.weather[i]["astronomy"][0]["moonset"]}</i></div>
</div>
</div>
</div>
},this )}
</div>
);
},
componentDidMount: function() {
return console.log(this.props.meteo.weather[0]["astronomy"][0]["sunrise"]);
},
});