Javascript &引用;“未定义”不是对象;,需要检查allNotes数组是否为空。怎么用?
感觉这是一个愚蠢的问题,但我只需要在allNotes不为空(它是一个数组)的情况下返回allNotes映射数据。这看起来只是一个简单的if/else,但实际上不知道如何在这个上下文(内部视图)中实现这一点。现在,当数组中没有任何内容时,我在标题中得到了错误。当它有数据时,一切都很好Javascript &引用;“未定义”不是对象;,需要检查allNotes数组是否为空。怎么用?,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,感觉这是一个愚蠢的问题,但我只需要在allNotes不为空(它是一个数组)的情况下返回allNotes映射数据。这看起来只是一个简单的if/else,但实际上不知道如何在这个上下文(内部视图)中实现这一点。现在,当数组中没有任何内容时,我在标题中得到了错误。当它有数据时,一切都很好 {isOpen && <TouchableOpacity onPress={() => updateDrop(prev => !prev)} styl
{isOpen &&
<TouchableOpacity onPress={() => updateDrop(prev => !prev)} style={styles.flastListUpdated}>
<View style={{ alignItems: 'center' }}>
<Text style={styles.flastlistItemText2}>{props.noteitem.note}</Text>
</View>
<View style={{ height: 1, width: '100%', backgroundColor: 'lightgrey' }} />
<View>
{allNotes.map((item) => {
return (
<View style={{ flexDirection: 'row', margin: 5 }}>
<View style={styles.perItemBar} />
<Text style={styles.noteTextStyle}>{item}</Text>
</View>
)
})}
<View style={styles.bottomBar}>
<TextInput onChangeText={(text) => { updateNoteStatus(text) }} style={{ flex: 2 }} placeholder='Enter New:' />
<TouchableOpacity onPress={addNote} style={{ flex: 1 }}>
<Text style={{ fontSize: 30 }}>+</Text>
</TouchableOpacity>
</View>
</View>
</TouchableOpacity>
}
</View>
)
}
{isOpen&&
updateDrop(prev=>!prev)}style={style.flastListUpdated}>
{props.noteitem.note}
{allNotes.map((项)=>{
返回(
{item}
)
})}
{updateNoteStatus(text)}style={{{flex:2}}}占位符='Enter New:'/>
+
}
)
}
我知道这很简单,但我不能得到正确的格式。我不知道这样做的正确方法是什么。谢谢 可以反复检查allNotes的定义是否是这样的,尽管最好将allNotes默认为空数组,然后在此处确认它是您所期望的
{isOpen &&
<TouchableOpacity onPress={() => updateDrop(prev => !prev)} style={styles.flastListUpdated}>
<View style={{ alignItems: 'center' }}>
<Text style={styles.flastlistItemText2}>{props.noteitem.note}</Text>
</View>
<View style={{ height: 1, width: '100%', backgroundColor: 'lightgrey' }} />
<View>
{allNotes && allNotes.map(item => (
<View style={{ flexDirection: 'row', margin: 5 }}>
<View style={styles.perItemBar} />
<Text style={styles.noteTextStyle}>{item}</Text>
</View>
)
)}
<View style={styles.bottomBar}>
<TextInput onChangeText={(text) => { updateNoteStatus(text) }} style={{ flex: 2 }} placeholder='Enter New:' />
<TouchableOpacity onPress={addNote} style={{ flex: 1 }}>
<Text style={{ fontSize: 30 }}>+</Text>
</TouchableOpacity>
</View>
</View>
</TouchableOpacity>
}
</View>
)
}
{isOpen&&
updateDrop(prev=>!prev)}style={style.flastListUpdated}>
{props.noteitem.note}
{allNotes&&allNotes.map(项=>(
{item}
)
)}
{updateNoteStatus(text)}style={{{flex:2}}}占位符='Enter New:'/>
+
}
)
}
你能告诉我你在哪里定义了allNotes
?allNotes
听起来像是未定义的,不是空数组。现在,假设该值为“空”时为“有效”值,则很容易将其合并为空数组。。迭代空数组实际上是不可操作的。所有注释都是在组件的全局级别定义的。看起来“let allNotes=props.noteitem.notes”undefined
是一个值,不一定是“未声明/未分配”。如果props.noteitem.notes
的值为undefined
,则应为该值。例如,如果props[.noteitem]
来自JSON,则缺少的属性将返回undefined
。IMOHO,最干净的方法是在引入点修复此数据模型。例如,props.noteitem.notes | |=[]
访问层中的某个地方;或者实际上从服务器/源返回一个空数组。