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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47652723/
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-10-29 07:31:54  来源:igfitidea点击:

react native converting array into string

javascriptreact-native

提问by DHC

I am trying to print out array's value, converting into string in the description of mapviewmarker. when I put <Text>nameList</text>inside of return()under <View style={styles.container}>is okay, but description={nameList.toString()}in is just print out {object,object}, {object,object}, {object,object}

我试图打印出数组的值,在mapview标记的描述中转换为字符串 。当我把<Text>nameList</text>里面放在return()下面的时候<View style={styles.container}>是好的,但里面description={nameList.toString()}只是打印出来{object,object}, {object,object}, {object,object}

Please let me know how would I solve.

请让我知道我将如何解决。

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>

 }

回答by Vipin Kumar

Use

利用

JSON.stringify(nameList)

instead of

代替

nameList.toString()

Source: https://www.w3schools.com/js/js_json_stringify.asp

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

Edited

已编辑

Change your map to

将您的地图更改为

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