React Native从状态数组获取特定的元素值

时间:2019-10-27 14:01:35

标签: arrays react-native

我是React-Native的新手,我正在构建一个示例应用程序,但是我对this.state.array有一个小问题,我希望从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 ..

2 个答案:

答案 0 :(得分:0)

我认为您想获得一个索引用户ID,所以我给出以下代码:

// define it in the constructor, and it may have more than one items
this.state.userDetail: [
   {
    "creation_Date": "2019-10-22T06:34:52.000Z",
    "mobile": 9985849955,
    "name": "siva",
    "password": "123456",
    "picture_url": "5.jpg",
    "role": "",
    "user_id": 1,
  },
]

// do not directly assign use this.state.x
let copyUserDetails = {...this.this.state.user_Details}
let userIdsArray = copyUserDetail.map((item) => { return item.user_id });
let itemId = userIdsArray[0]
console.log("the first user Id is",itemId)
// if you only want to the user_id, you can directly copyUserDetails[0].user_id
let userId = copyUserDetails[0].user_id
console.log("the first user Id is  ==> ",userId)
this.setState({ user_id: itemID })


// the filter is accordng some condtion to return a new arry
let  itemFilterArray = copyUserDetails.filter((element,i) => { 
  // filter the item which match some condition, for example the uerId not equal 
  //0 
  if(element.user_id !== 0){
       return element
    }
})

根据您的要求,我提供以下代码:

//if you do not have many userInfo
constructor(props){
   super(props)
   this.state= {
   user_Details: {
    "creation_Date": "2019-10-22T06:34:52.000Z",
    "mobile": 9985849955,
    "name": "siva",
    "password": "123456",
    "picture_url": "5.jpg",
    "role": "",
    "user_id": 1,
    }
  }

  // somewhere you want the userId
  getUserId = () => {
      return this.state.user_Details.user_id
   }





}
//if you have many userinfo
constructor(props){
   super(props)
   this.state= {
   user_Details: [{
    "creation_Date": "2019-10-22T06:34:52.000Z",
    "mobile": 9985849955,
    "name": "siva",
    "password": "123456",
    "picture_url": "5.jpg",
    "role": "",
    "user_id": 1,
    }]
  }

  // somewhere you want the some index userId
  getUserId = (index) => {
     // do not directly assign use this.state.x
     let copyUserDetails = {...this.this.state.user_Details}
     return copyUserDetails[index].user_id
   }
   //somwhere you want all the userId
  getUserId = () => {
      // do not directly assign use this.state.x
     let copyUserDetails = {...this.this.state.user_Details}
     let userIdArray = copyUserDetail.map((item) => { return item.user_id });
     return userIdArray
   }
}




我建议您可以阅读有关jsonarray的api

答案 1 :(得分:0)

如果您希望从此示例中特定地提取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()函数的特定条目。