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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38642411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:32:03  来源:igfitidea点击:

How to map json data with array in react native

arraysjsonrestreact-native

提问by Sugeivan Senthinathan

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 mapid to key and name to label???

如何map识别键和命名标签???

采纳答案by lalkmim

You want to fill your const datawith values from JSON, correct?

您想const data用 JSON 中的值填充您的值,对吗?

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);

回答by Sandhya Deshmukh

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);

回答by Aakash Sajjad

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 中获取数据数组,并将数据映射到下拉/选择器,我希望它对其他人有帮助,如果您有任何疑问,请向我询问