如何将json数据映射到本机中的数组

时间:2016-07-28 16:52:10

标签: arrays json rest react-native

我有像这样的数组反应原生

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

和json数据:

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

如何map id键入并命名为标签???

3 个答案:

答案 0 :(得分:3)

您希望使用JSON中的值填充const data,对吗?

试试这个:

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

答案 1 :(得分:1)

如果您的数据如下(已删除服务键)

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

答案 2 :(得分:-1)

我很晚才知道它,但是我希望它对其他人有帮助,如何在react native中获取JSON数组的响应?如何在react native中将json数据与array映射

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>


)
}


}

上面的例子是从json获取数据数组,并将数据映射到下拉列表/ picker,希望对其他人有帮助,如果您有任何疑问,请问我