.push() 在 for 循环之外不起作用
.push() is not working outside of the for loop
我已经处理了来自 API 呼叫的响应。然后我将数据导出到我在内部使用 for
循环的函数中。 const diomerda
被传递到我从 API 输出数组数据的 ejs 文件。但是数组中的数据超过 5000 个条目,所以我想 运行 一个 for 循环来用整个数组填充 ejs 文件。但是当我使用 loop.push
时,它不会填充 const loop
。 console.log(loop)
只读 []
.
一旦 const loop = []
填充了数组,我就会将数据传递到 const diomerda = [json.entries[loop].character.name]
如果有人对如何实现我想要做的事情有任何其他想法,请随时发送。提前致谢。
.then(response => response.json())
.then(json => ejsoutput(json))
}
function ejsoutput(json) {
const loop = []
console.log(loop)
const diomerda = [json.entries[0].character.name, json.entries[1].character.name, json.entries[2].character.name]
for (var i = 0; i < json.entries.length; i++) {
loop.push(i)
}
res.render('index', {
leaderboard: diomerda
})
}
});
要调试,打印(记录)json 对象,而不是空循环数组(应该让您步入正轨)。
Json应该是一个承诺。这可能就是您 运行 的兴趣所在。
参见:https://developer.mozilla.org/en-US/docs/Web/API/Response/json
试试
json().then(function(json) {
});
Once the const loop = []
is populated with the array i was then
going to pass the data into the const diomerda = [json.entries[loop].character.name]
如果您想要用 api 中的每个角色名称 return 填充 diomerda
,请执行此操作
function ejsoutput(json) {
const diomerda = [];
for (var i = 0; i < json.entries.length; i++) {
diomerda.push(json.entries[i].character.name);
}
console.log(diomerda);
}
或更简单的说法
function ejsoutput(json) {
const diomerda = json.entries.map(item => item.character.name);
console.log(diomerda);
}
我已经处理了来自 API 呼叫的响应。然后我将数据导出到我在内部使用 for
循环的函数中。 const diomerda
被传递到我从 API 输出数组数据的 ejs 文件。但是数组中的数据超过 5000 个条目,所以我想 运行 一个 for 循环来用整个数组填充 ejs 文件。但是当我使用 loop.push
时,它不会填充 const loop
。 console.log(loop)
只读 []
.
一旦 const loop = []
填充了数组,我就会将数据传递到 const diomerda = [json.entries[loop].character.name]
如果有人对如何实现我想要做的事情有任何其他想法,请随时发送。提前致谢。
.then(response => response.json())
.then(json => ejsoutput(json))
}
function ejsoutput(json) {
const loop = []
console.log(loop)
const diomerda = [json.entries[0].character.name, json.entries[1].character.name, json.entries[2].character.name]
for (var i = 0; i < json.entries.length; i++) {
loop.push(i)
}
res.render('index', {
leaderboard: diomerda
})
}
});
要调试,打印(记录)json 对象,而不是空循环数组(应该让您步入正轨)。
Json应该是一个承诺。这可能就是您 运行 的兴趣所在。 参见:https://developer.mozilla.org/en-US/docs/Web/API/Response/json
试试
json().then(function(json) {
});
Once the
const loop = []
is populated with the array i was then going to pass the data into theconst diomerda = [json.entries[loop].character.name]
如果您想要用 api 中的每个角色名称 return 填充 diomerda
,请执行此操作
function ejsoutput(json) {
const diomerda = [];
for (var i = 0; i < json.entries.length; i++) {
diomerda.push(json.entries[i].character.name);
}
console.log(diomerda);
}
或更简单的说法
function ejsoutput(json) {
const diomerda = json.entries.map(item => item.character.name);
console.log(diomerda);
}