将原生转换数组反应为字符串

时间:2017-12-05 11:38:47

标签: javascript react-native

我正在尝试打印数组的值,在mapview标记的描述中转换为字符串。 当我将<Text>nameList</text>置于return() <View style={styles.container}>下方时,description={nameList.toString()}只是打印出{object,object}, {object,object}, {object,object}

请告诉我如何解决。

constructor(){
super()
this.state = {name: []}

}

  componentDidMount(){

   return fetch('https://transportapi.com/v3/uk/bus/stop/6090282/live.jsonapp_id=8425e834&app_key=a9de6fe50081ecf38d2e9c9d5f1a87e0&group=route&nextbuses=yes')
.then((response) => response.json())
.then((responseJson) => {
for(var x in responseJson.departures){
this.setState({ 
state : this.state["name"].push(responseJson.departures[x][0])

});
}



   })
.catch((error) => {
  console.error(error);
});
  }




render() {


const info = this.state.name;
const nameList = info.map(name =>{
  return(

    <Text key={name.line}>{name.line}{name.aimed_departure_time}</Text>
  ) 

})

 return (

  <View style={styles.container}>
    <MapView
initialRegion={{
  latitude: 37.78825,
  longitude: -122.4324,
  latitudeDelta: 0.0922,
  longitudeDelta: 0.0421,
}}
>

  <MapView.Marker
   coordinate={{
   latitude: 37.78825,
  longitude: -122.4324,
   }}
   title={'Hello'}
   description={nameList.toString()}

   />

    </MapView>

 </View>

 }

1 个答案:

答案 0 :(得分:3)

使用

JSON.stringify(nameList)

而不是

nameList.toString()

来源:https://www.w3schools.com/js/js_json_stringify.asp

<强>被修改

将地图更改为

const nameList = info.map(name => {
  return `${name.line} : ${name.aimed_departure_time}`
})