Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取数组中的位置_Javascript_Arrays - Fatal编程技术网

Javascript 获取数组中的位置

Javascript 获取数组中的位置,javascript,arrays,Javascript,Arrays,我收到来自API的响应,但我无法访问此响应中数组的位置 fileTransfer.upload(this.imageURI, environment.restUrl + "Upload/addIonic", options) .then(data => { console.log('data'); console.log(JSON.stringify(data)); let response; console.lo

我收到来自API的响应,但我无法访问此响应中数组的位置

fileTransfer.upload(this.imageURI, environment.restUrl + "Upload/addIonic", options)
      .then(data => {
        console.log('data');
        console.log(JSON.stringify(data));
        let response;
        console.log('bytesSent');
        console.log(data.bytesSent);
        response = data.response
        console.log('response');
        console.log(response);
        console.log('response[0]');
        console.log(response[0]);
});
控制台返回:

console.log: bytesSent
[19:51:03]  console.log: 1168539
[19:51:03]  console.log: response
[19:51:03]  console.log:
            [{"fd":"445bcc46-ad55-4079-95d0-9b0deaab7c4c","size":1168430,"type":"image/jpeg","filename":"ionicfile","status":"finished","field":"ionicfile","extra":{"Location":"https://easy-move.s3.amazonaws.com/445bcc46-ad55-4079-95d0-9b0deaab7c4c","Bucket":"easy-move","Key":"445bcc46-ad55-4079-95d0-9b0deaab7c4c","ETag":"\"69ede2190589f905ee8590446caf1cf7-1\"","size":1168430}}]
[19:51:03]  console.log: response[0]
[19:51:03]  console.log: [

相反,返回一个对象,数组只返回零位的“[”。

response
显然是一个JSON字符串,而不是数组,因此
response[0]
返回字符串的第一个字符。使用:

response = JSON.parse(data.response);

如果您对API有控制权,您应该调查它为什么要对该元素进行编码,而不是在对整个数据进行编码时将其作为数组。很少需要这样的双重编码。

response
显然是一个JSON字符串,而不是数组,因此
response[0]
返回字符串的第一个字符。使用:

response = JSON.parse(data.response);

如果您对API有控制权,您应该调查它为什么对该元素进行编码,而不是在对整个数据进行编码时将其作为数组。很少需要这样的双重编码。

response
是一个字符串,您需要使用
JSON.parse()
将其转换为数组。
response
是一个字符串,您需要使用
JSON.parse()
将其转换为数组。