arrays - React Native 从状态数组中获取特定的元素值
问题描述
我是 React-Native 的新手,我正在构建一个示例应用程序,但是我对 this.state.array 有一个小问题,我想要数组中的一个特定元素值我的数组是
this.state.userDetail: [
Object {
"creation_Date": "2019-10-22T06:34:52.000Z",
"mobile": 9985849955,
"name": "siva",
"password": "123456",
"picture_url": "5.jpg",
"role": "",
"user_id": 1,
},
]
````````````````````````````````````````
In the above array i want user_id value ,
i tried different methods like
```````````````````````````
this.setState({ user_id: this.state.user_Details.user_id })
const item_id = this.state.user_Details.map((item) => { item.user_id });
var item_id = this.state.user_Details.filter(userDetails => { return userDetails[7]; })
``````````````````````````````````````````
but nothing will work i want only user_id value to update the users table , So please any help ..
解决方案
如果您希望从此示例中特别提取 user_id:
[
{
"creation_Date": "2019-10-22T06:34:52.000Z",
"mobile": 9985849955,
"name": "siva",
"password": "123456",
"picture_url": "5.jpg",
"role": "",
"user_id": 1,
},
]
并假设此数据在您的this.state.userDetail
财产中。你唯一需要的是:
this.state.userDetail[0].user_id
为什么这可能不适合您:
this.state.userDetail: [
Object { /// What's Object?
如果您尝试解析数组中的多个条目,与您的示例不同,您首先需要使用“for”循环或 .map() 函数选择某个条目。
推荐阅读
- azure - Azure Web App Bot - 链接到另一个帐户的知识库
- python - 在c中将枚举作为函数参数传递
- php - Builder::whereBetween(): 参数 #2 ($values) 必须是数组类型
- amazon-dynamodb - DynamoDB 和 RDS Mysql
- ios - 无法获取 CallKit 开发者文档中提到的调用者信息
- laravel - 如何在 s3 中读取图像的宽度、高度、文件大小?
- python - 在 python 中更新 config.json 中的值
- linux - TLS 握手失败。对HAproxy的HTTPS请求到http然后再次加密转发请求到ssl服务器
- java - JAVA - 如何使用签名方法为 HMAC-SHA256 为 OAuth 1.0 REST API 构建客户端?
- python-3.x - 无法在 Python 中使用 selenium Web 驱动程序单击展开按钮