[英]Cannot access properties inside objects
In my react app i'm mapping an array like below在我的反应应用程序中,我正在映射一个如下所示的数组
{(newdata !== null) ? newdata && newdata.map((item,key) => {
console.log("item ",item);
return (
<div>{item.image_location}</div>
)
}
):null
}
And my console log looks like this我的控制台日志看起来像这样
But cannot object the image_location
attribute or any other attributes in the item object.但不能 object 项目 object 中的
image_location
属性或任何其他属性。 i tried item.image_location
but it returns a undefined
.我试过
item.image_location
但它返回一个undefined
。 How to access the attributes inside my item
object如何访问我的
item
object 中的属性
I tried accessing like this我尝试像这样访问
const newkey = parseInt(key)+1; console.log("item ",item.$['place_'+newkey]);
Still i get a undefined error我仍然得到一个未定义的错误
LATEST CODE最新代码
{(newdata !== null) ? newdata && newdata.map((item, key) => {
var newkey = parseInt(key) + 1;
console.log("item ", item['place_' + newkey]);
return (
<Row noGutters className="container_one polaroid unchange_div" key={key}>
<Col xs={4} className="attraction_container">
<img src={item.image_location} alt="img"
className="attraction_img"/>
</Col>
</Row>
)
}) : (countrydata === null && loading === false) ?
<div className="horiz_center"><span className="nodata_lbl">No data</span>
</div> : null
}
In the log, you can clearly see that, the image_location
attribute is wrapped inside place_1
attribute.在日志中,您可以清楚地看到,
image_location
属性包含在place_1
属性中。 So, you have to access it like item.place_1.image_location
因此,您必须像
item.place_1.image_location
一样访问它
Update更新
{(newdata !== null) ? newdata && Object.keys(newdata).sort((a,b) => parseInt(a.split('_')[1]) - parseInt(b.split('_')[1])).forEach((key) => {
console.log("item ", newdata[key]);
return (
<Row noGutters className="container_one polaroid unchange_div" key={key}>
<Col xs={4} className="attraction_container">
<img src={newdata[key].image_location} alt="img" className="attraction_img"/>
</Col>
</Row>
)
}) : (countrydata === null && loading === false) ?
<div className="horiz_center"><span className="nodata_lbl">No data</span>
</div> : null
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.