Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 错误:对象作为React子对象无效(找到:具有键的对象………)_Javascript_Arrays_Reactjs - Fatal编程技术网

Javascript 错误:对象作为React子对象无效(找到:具有键的对象………)

Javascript 错误:对象作为React子对象无效(找到:具有键的对象………),javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我试图在对象数组中循环。控制台上this.state.saveFriendTag或this.props.userTags的值为: 构造函数中的状态为: saveFriendTag:this.props.userTags?this.props.userTags:[] 代码是: if(this.props.cardData){ if (this.state.saveFriendTag.length == 1) { taggedFriendsBlue = this.state.save

我试图在对象数组中循环。控制台上this.state.saveFriendTag或this.props.userTags的值为:

构造函数中的状态为: saveFriendTag:this.props.userTags?this.props.userTags:[]

代码是:

if(this.props.cardData){
  if (this.state.saveFriendTag.length == 1) {
    taggedFriendsBlue =  this.state.saveFriendTag.map((item, index) => {
      console.log(item,"item");
      return (
        <span className="displayedName blue" key={index}>{item.firstname}</span>
      )
    })
  } 
if(this.props.cardData){
if(this.state.saveFriendTag.length==1){
taggedFriendsBlue=this.state.saveFriendTag.map((项目,索引)=>{
控制台日志(项目,“项目”);
返回(
{item.firstname}
)
})
} 

这将作为回报,taggedFriendsBlue在render中定义:

 <div className="pCard_contentContainer ">
     <textarea id="pcardTextarea" type="text" placeholder="Write Description here..." value={this.state.Description} onChange={this.textareaExpansion.bind(this)}></textarea>
       <If test={this.state.tagDone == true || this.state.saveFriendTag.length>0}>
       <div className="displayNames disp_inliFl">
         <span className="pcard_WithOne">-With</span>
           <div className="disp_inliFl">
               {taggedFriendsBlue}
           </div>
       </div>
    </If>
  </div>

0}>
-与
{taggedFriendsBlue}

有人能说出这个控制台错误的原因吗?如何更正它?

问题似乎是您在数组上使用了
Object.keys
。您可以删除它,直接使用
.map

此外,还需要为
span
指定一个键

const数组=[
{
名字:“测试”,
姓氏:“test2”
}
];

log(Object.keys(数组))能否显示组件的完整呈现方法?如果尝试迭代的集合是数组,则不必指定
对象。keys
@YogeshDevgun你在问什么?@YogeshDevgun别忘了在div上包含
key
属性。另外,在刚才注释的代码段中,
是一个对象。因此,您应该执行类似于
item.firstName
之类的操作来获取值,而不是单独使用对象。@Mathew很抱歉上一篇文章
taggedFriendsBlue=this.state.saveFriendTag.map((项,索引)=>{console.log(项,项”);return({item.firstname})})
这就是我现在使用的。我还向span添加了键,并删除了直接映射数组的Object.Keys。但控制台上的相同错误是,这对您有效还是您仍然看到错误?