首页 > 解决方案 > 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 ..

标签: arraysreact-native

解决方案


如果您希望从此示例中特别提取 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() 函数选择某个条目。


推荐阅读