简体   繁体   English

如何在本机反应中将json数据与数组映射

[英]How to map json data with array in react native

I have array like this in react native我在本机反应中有这样的数组

 const data = [
    { key: 1, label: 'Service1'},
    { key: 2, label: 'Service2' },
    { key: 3, label: 'Service3' },
    { key: 4, label: 'Service4' },
    { key: 5, label: 'Service4' },
 ];

and json data:和json数据:

 "services": [
    {
      "id": 1,
      "name": "Hotels",
    },
    {
      "id": 2,
      "name": "Embassies",
    },
 ]

How to map id to key and name to label???如何将 id map到键并将名称map到标签???

You want to fill your const data with values from JSON, correct?您想用 JSON 中的值填充const data ,对吗?

Try this:试试这个:

 var jsonData = { "services": [ { "id": 1, "name": "Hotels" }, { "id": 2, "name": "Embassies" } ] }; var data = jsonData.services.map(function(item) { return { key: item.id, label: item.name }; }); console.log(data);

if your data like below (removed services key)如果您的数据如下(删除了服务密钥)

var jsonData = [
    { "id": 1, "name": "Hotels" },
    { "id": 2, "name": "Embassies" }
  ];

var data = jsonData.map(function(item) {
  return {
    key: item.id,
    label: item.name
  };
});

console.log(data);

i know it to much late,but i hope its helpfull for others,How to fetch the response of JSON array in react native?How to map json data with array in react native我知道它很晚了,但我希望它对其他人有帮助,如何在 react native 中获取 JSON 数组的响应?如何在 react native 中将 json 数据与数组映射

export default class ExpenseNew extends Component {
    constructor(){
        super();
        this.state={
            PickerSelectedVal : '',
            accountnameMain:[],
        }
    }
     componentDidMount(){
         var account_nam=[]
                fetch('your Url', {
                    method: 'GET',
                    headers: { 'Authorization': 'Bearer ' + your token }
                })
                    .then((response) => response.json())
                    .then((customerselect) => {
                        // alert(JSON.stringify(customerselect)) 
                        global.customerdata = JSON.stringify(customerselect)
                        var customername = JSON.parse(customerdata);
                        //alert(JSON.stringify(customername));
                        for (i = 0; i < customername.cus_data.length; i++) {
                            var dataa = customername.cus_data[i]["account_name"];  
                            account_nam.push(dataa)
                        }
                        this.setState({accountnameMain:account_nam});
                    })
                    .done();
    }
                    render() {
                          return (
                               <Picker
                                selectedValue={this.state.PickerSelectedVal}
                                placeholder="Select your customer"
                                mode="dropdown"
                                iosIcon={<Icon name="arrow-down" />}
                                onValueChange={(itemValue, itemIndex) => this.setState({PickerSelectedVal: itemValue})} >

                                {this.state.accountnameMain.map((item, key)=>(
                                    <Picker.Item label={item} value={item} key={key}/>)
                                )}

                            </Picker>


)
}


}

the above example is fetch array of data from json,and map data in to dropdown/picker,i hope its helpfull for others,if you have any query, asked from me上面的例子是从 json 中获取数据数组,并将数据映射到下拉/选择器,我希望它对其他人有帮助,如果您有任何疑问,请向我询问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM