[英]Decosnstructing object in react-native
I have this kind of structure:我有这种结构:
I want to acces uri as key and its value as value.我想访问 uri 作为键并将其值作为值。 How i should do it?
我应该怎么做?
This is what I tried and the key I get is images rest of the data is value这是我尝试过的,我得到的关键是数据的图像rest 是值
const ImagesToDisplay = images => {
let imagesMap = Object.entries(images);
console.log('Images object' + JSON.stringify(images));
return (
<View>
{imagesMap.map(([key, value]) => (
// <Image style={styles.avatar} key={key} source={key + value} />
<Text>{key}</Text>
))}
</View>
);
};
I need to deconstruct this data so i could store it as image, because there is base64 image.我需要解构这些数据,以便将其存储为图像,因为有 base64 图像。 I have commented my Image component now, for debugging
我现在已经评论了我的 Image 组件,用于调试
This is how i read data from firebase:这就是我从 firebase 读取数据的方式:
readUserData() {
firebase
.database()
.ref('/users/' + firebase.auth().currentUser.uid)
.once('value')
.then(snapshot => {
let data = snapshot.val();
this.setState({
id: data.fbid,
profilePictureUrl: data.profile_picture,
nickname: data.nickname,
userInterest: data.userInterest,
description: data.description,
images: data.images,
});
// console.log(data);
});
}
to ImagesToDisplay i pass this.state.images Console.log(images) looks like this到ImagesToDisplay我通过 this.state.images Console.log(images) 看起来像这样
uri is base64 so its very long
uri 是 base64 所以它很长
well, it looks like the key will be "M..." image name, i think in your case it will work if you do好吧,看起来键将是“M ...”图像名称,我认为在您的情况下,如果您这样做,它将起作用
Object.values(images).map(el => { return <div key={Object.entries(el)[0][0]}>{Object.entries(el)[0][1]}</div>)})
It will be much clearer if you posted the actual images array that you receive from db如果您发布从 db 收到的实际图像数组,会更清楚
You could use Object.values(images).map(obj => Object.entries(obj)).flat();
你可以使用
Object.values(images).map(obj => Object.entries(obj)).flat();
Example:例子:
const images = { 'jssjsks': { uri: 'uri1' }, 'wjqwhqwj': { uri: 'uri2' } }; let imagesMap = Object.values(images).map(obj => Object.entries(obj)).flat(); // 1. `Object.values(images)` returns `[{ uri: 'uri1' }, { uri: 'uri2' }]` // 2. Then `.map(obj => Object.entries(obj))` returns `[[['uri', 'uri1']], [['uri', 'uri2']]]` // 3. Finally `.flat()` produces the wanted result `[['uri', 'uri1'], ['uri', 'uri2']]` // console.log(imagesMap) imagesMap.forEach(([key, value]) => { console.log(key) console.log(value) })
Thanks for help I edited the method, so in Object.entries I pass images.images and it solves problem.感谢您的帮助,我编辑了该方法,因此在 Object.entries 中,我通过了 images.images 并解决了问题。
const ImagesToDisplay = images => {
let imagesMap = Object.entries(images.images);
// console.log('Images object' + JSON.stringify(images.images));
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
{imagesMap.map(([key, value]) => {
return <Image style={styles.avatar} key={key} source={value} />;
// <Text style={styles.avatar}>{key}</Text>;
})}
</View>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.