This question already has answers here:
How do I return the response from an asynchronous call?
(38个答案)
2年前关闭。
我知道这件事已经受到质疑,但实际上,我还没有发现任何有用或可理解的东西。
我有以下代码:
我期望的是
先感谢您。
至
(不确定100%,也许甚至是
(38个答案)
2年前关闭。
我知道这件事已经受到质疑,但实际上,我还没有发现任何有用或可理解的东西。
我有以下代码:
var pdfListPath = [];
function listDir(path) {
window.resolveLocalFileSystemURL(path,
function(fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function(entries) {
console.log(entries);
for (var i = 0; i == entries.length; i++) {
pdfListPath.push(entries[i]);
};
},
function(err) {
console.log(err)
}
);
},
function(err) {
console.log(err);
}
);
};
var pdfList = listDir(cordova.file.applicationDirectory + "www/pdf/");
console.log(pdfList);
console.log(pdfListPath);
我期望的是
pdfListPath
充满了东西,但是我在控制台中看到的是“未定义”。entries
而是填充数组,所以这有什么问题呢?先感谢您。
最佳答案
您的for循环是错误的,您需要更改
for (var i = 0; i == entries.length; i++) {
pdfListPath.push(entries[i]);
};
至
for (var i = 0; i < entries.length; i++) {
pdfListPath.push(entries[i]);
};
(不确定100%,也许甚至是
<=
您最好检查i
和entries.length
)关于javascript - JavaScript push返回一个空数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45840457/