[英]Fetch keys and values from API array react native
I'm trying to access both keys and also values in an array of an API我正在尝试访问 API 数组中的键和值
export default const App= () =>{
const [data, setData] = useState([]);
console.log(data);
//Some fetch code here
//setData happen here
return (
<View >
<Text >fetched data</Text>
<Text>{data?.data?.a}</Text>
<Text>{data?.data?.b}</Text>
<Text>{data?.data?.c}</Text>
</View>
)
};
the supposed array is like this假设的数组是这样的
arr = {code: 0, data:{a: 1, b: 2, c: 3}}
but it will only return 1, 2, 3 and I wanted it to be a: 1, b: 2, c: 3但它只会返回 1, 2, 3 我希望它是 a: 1, b: 2, c: 3
You can access the Object.entries您可以访问 Object.entries
const arr = {code: 0, data:{a: 1, b: 2, c: 3}} console.log(`<Text>${Object.entries(arr.data).map(([key,val])=>`${key}:${val}`).join('</Text></Text>')}</Text>`)
Try this尝试这个
<View>
<Text>fetched data</Text>
{Object.entries(arr?.data).map(([key, val]) => {
<Text>{`${key}:${val}`}</Text>;
})}
</View>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.