如何在javascript中数组到字符串?
我试图让string方法处理数组,但我一直得到一个空字符串如何在javascript中数组到字符串?,javascript,arrays,json,string,Javascript,Arrays,Json,String,我试图让string方法处理数组,但我一直得到一个空字符串 let url = "https://api.myjson.com/bins/be7fc" let data =[]; fetch(url) .then(response => response.json()) .then(result => data.push(result[0].id)); //Array console.log(data); // to str
let url = "https://api.myjson.com/bins/be7fc"
let data =[];
fetch(url)
.then(response => response.json())
.then(result => data.push(result[0].id));
//Array
console.log(data);
// to string
let x = data.toString();
console.log(x);
我不确定我缺少了什么使用join而不是toString(),你应该在抓取成功模式下执行。因为在异步部分,在您从api获取数据之前,控制台是第一位的
let url = "https://api.myjson.com/bins/be7fc"
let data =[];
fetch(url)
.then(response => response.json())
.then(result => {data.push(result[0].id);console.log(data.join(","))});
问题是,您的数组在
log
时为空,但在单击展开时填充。我肯定有一份副本,解释得很好,但我还在找。解释了Chrome控制台的扩展行为,但并未真正涉及到更一般的异步问题(即,如果您使用的是then
内部生成的值,则不应在then
之外执行任何操作)