英文:
ERROR TypeError: undefined is not a function Why my object is not converting to array?
问题
ERROR TypeError: undefined is not a function 为什么我的对象无法转换为数组?这是我面临的错误,我试图映射当前为对象的所有项目,但我想将它们转换为数组以进行映射,我该如何做到这一点?它尚未转换为数组,我尝试了一些互联网上的解决方案,我该如何做到这一点?
Currenlty其给我null,当我首次使用const propertyValues = Object.values(item);时,它给我了ERROR TypeError:undefined is not a function错误
const renderImages = ({ item, index }) => {
console.log(item)
if (!item || !Array.isArray(item)) {
return null;
}
const propertyValues = Object.values(item);
console.log(propertyValues);
return (
<View style={styles.images}>
{item.map((url) => (
<Image key={url} style={styles.pic} source={{ uri: url }} />
))}
</View>
);
};
如果我什么都不做,只是记录item,它会记录类似这样的东西:
LOG {"picture": "https://firebasestorage.googleapis.com/v0/b/myprojectfirebase.appspot.com/o/file%3A%2Fdata%2Fuser%2F0%2Fhost.exp.exponent%2Fcache%2FExperienceData%2F%252540anonymous%25252FYounme-4dc528f7-0647-403c-9671-6a595b71bf0b%2FImagePicker%2Fba5eb500-c4e3-4199-9cef-5a8bfb3b1219.jpeg?alt=media&token=49041038-deef-40d9-9809-41210982ae36"}
{
userdata ?
<FlashList
data={userdata.pictures}
renderItem={renderImages}
keyExtractor={(item, index) => index.toString()}
getItemCount={() => userdata.pictures.length}
getItem={(data, index) => data[index]}
initialNumToRender={5}
windowSize={5}
ListHeaderComponent={
...//
}
}
/>
英文:
ERROR TypeError: undefined is not a function Why my object is not converting to array? this is the error that I am facing I am trying to map all the item which is currently object but I want to convert it to array to map all them how can I do that? its not been converted into array I tried some solutions on internet how can I do that?
Currenlty its giving me null and when i first const propertyValues = Object.values(item); it gave me ERROR TypeError: undefined is not a function error
const renderImages = ({ item, index }) => {
console.log(item)
if (!item || !Array.isArray(item)) {
return null;
}
const propertyValues = Object.values(item);
console.log(propertyValues);
return (
<View style={styles.images}>
{item.map((url) => (
<Image key={url} style={styles.pic} source={{ uri: url }} />
))}
</View>
);
};
If i do nothing just log the item it logs something like this:
LOG {"picture": "https://firebasestorage.googleapis.com/v0/b/myprojectfirebase.appspot.com/o/file%3A%2Fdata%2Fuser%2F0%2Fhost.exp.exponent%2Fcache%2FExperienceData%2F%252540anonymous%25252FYounme-4dc528f7-0647-403c-9671-6a595b71bf0b%2FImagePicker%2Fba5eb500-c4e3-4199-9cef-5a8bfb3b1219.jpeg?alt=media&token=49041038-deef-40d9-9809-41210982ae36"}
{
userdata ?
<FlashList
data={userdata.pictures}
renderItem={renderImages}
keyExtractor={(item, index) => index.toString()}
getItemCount={() => userdata.pictures.length}
getItem={(data, index) => data[index]}
initialNumToRender={5}
windowSize={5}
ListHeaderComponent={
...//
}
}
/>
答案1
得分: 1
如果item
是一个对象,此代码将始终返回null
:
if (!item || !Array.isArray(item)) {
return null;
}
此外,如果你想创建一个URL数组,你可以简单地使用Object.keys
和.map
来创建一个item
URL 数组。
const properties = Object.keys(item);
const values = properties.map((p) => item[p].url);
由于item
只有一个picture
,为什么不简化:
const renderImages = ({ item, index }) => {
return (
<View style={styles.images}>
<Image key={item.picture} style={styles.pic} source={{ uri: item.picture }} />
</View>
);
};
英文:
If item
is an object, this code will always return null
:
if (!item || !Array.isArray(item)) {
return null;
}
Additionally, if you want to create an array of URLs, you can simply use Object.keys
and .map
to create an array of item
urls.
const properties = Object.keys(item);
const values = properties.map((p) => item.url);
Since item
just has one picture
, why not simplify:
const renderImages = ({ item, index }) => {
return (
<View style={styles.images}>
<Image key={item.picture} style={styles.pic} source={{ uri: item.picture }} />
</View>
);
};
答案2
得分: 1
考虑执行:
const renderImages = ({ item, index }) => {
return (
<View style={styles.images}>
<Image key={url.picture} style={styles.pic} source={{ uri: url.picture }} />
</View>
);
};
英文:
Consider doing:
const renderImages = ({ item, index }) => {
return (
<View style={styles.images}>
<Image key={url.picture} style={styles.pic} source={{ uri: url.picture }} />
</View>
);
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论