从对象数组中获取 2 个值 [英] Getting 2 values from array of objects

查看:53
本文介绍了从对象数组中获取 2 个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从下拉列表中选择一个项目,并想为它设置 staffId 的状态.我希望下拉列表显示名称,但在选择名称后,将人员 ID 设置为 state.我还想实现这一点,而无需在 if-else 循环中对值进行硬编码,因为我有成千上万的数据要处理.

这是从 WebAPI 获取并存储在称为 items 的状态中的数据

this.setState({项目:数据源员工编号:''})

物品的状态就是这样.

<预><代码>[{"departmentName":"HOUSE","employeeName":"TEST1", "staffId": "00001"},{"departmentName":"HOUSE","employeeName":"TEST2", "staffId": "00002"},{"departmentName":"HOUSE","employeeName":"TEST3", "staffId": "00003"}]

我在 react native 中创建了一个下拉列表,其中只有 emplyeeName.我正在为库使用 react-native-material-dropdown.我想显示employeeName,但是当用户选择一个名字时,得到一个id

let staff = this.state.listOfEmployees.map(item => ({值:item.employeeName员工 ID: item.staffId}));

this.onChangeName(staffId)}/>

这里在这个函数中,我想将staffId的状态设置为选中值的staffId.

onChangeName = (staffId) =>{//设置staffId的状态为选中值的staffId.//例如,如果选择AUNG THU 1,则staffId的状态为'00001'.this.setState({员工编号:员工编号})}

解决方案

您可以使用 valueExtractorlabelExtractor 道具执行此操作.应该看起来像这样:

员工编号}labelExtractor={({employeeName }) =>员工姓名}onChangeText={(staffId) =>this.onChangeName(staffId)}/>

I would like to select an item from the dropdown list and would like to set the state of staffId to it. I would like dropdown to show the name, but after I select the name, set the staffid to state. I would also like to achieve this without hardcoding the values in an if-else loop as I have thousands of data to work with.

This is the data fetched from WebAPI and stored in a state called items

this.setState({
    items: dataSource
  staffId: ''
})

This state of items will be this.

[
    {"departmentName":"HOUSE","employeeName":"TEST1", "staffId" : "00001"},
    {"departmentName":"HOUSE","employeeName":"TEST2", "staffId" : "00002"},
    {"departmentName":"HOUSE","employeeName":"TEST3", "staffId" : "00003"}
]

I have created a dropdown in react native with just the emplyeeName. I am using react-native-material-dropdown for the library. I would like to show employeeName but when a user select a name, get an id instead

let staff = this.state.listOfEmployees.map(item => ({
        value: item.employeeName
        staffId: item.staffId
      }));

<Dropdown
    label='Staff Name'
    baseColor='#0079b5'
    data={staff}
    selectedValue={value}
    onChangeText={(staffId) => this.onChangeName(staffId)}
/>

Here in this function, I would like to set the state of staffId to the staffId of the selected value.

onChangeName = (staffId) => {
    //set the state of staffId to the staffId of the selected value.
    //For Example, if AUNG THU 1 is selected, the state of staffId is '00001'.
    this.setState({
       staffId: staffId
    })
}

解决方案

You can do this using the valueExtractor and labelExtractor props. Should look something like this:

<Dropdown
  label='Staff Name'
  baseColor='#0079b5'
  data={staff}
  selectedValue={value}
  valueExtractor={({ staffId }) => staffId}
  labelExtractor={({ employeeName }) => employeeName}
  onChangeText={(staffId) => this.onChangeName(staffId)}
/>

这篇关于从对象数组中获取 2 个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆