简体   繁体   English

无法访问对象内部的属性

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM