当向 API 发出 AJAX GET 请求后满足某些条件时,我尝试打印出长度。我不确定我是否做得正确。我不确定你是否可以帮助我,但我想我会尝试一下,因为我被困住了。
该代码表示当 UserID 与 LocalStorage UserID 匹配时,循环遍历 IsClosed 为 False 且问题在过去 24 小时内发生的所有问题。另外, isAfter 也不起作用。有人可以对此提出建议吗?
此外,如果您需要我提供任何其他信息,请告诉我。谢谢。
function showNotificationCount(isLive, userIDNum, baseURL) {
var userIDLogin = localStorage['userIDNum'];
var userFullName = localStorage['userName'];
// var webMethod = isLive ?
$.ajax({
type: "GET",
url: webMethod,
contentType: "application/json; charset=utf-8",
dataType: "JSON",
data: {
userID: userIDNum
},
success: function(data) {
var parsedData = JSON.parse(data);
var twentyFourHoursAgo = moment().subtract(1, 'day');
console.log(parsedData);
if (userIDLogin == userIDNum) {
$.each(parsedData, function(index, item) {
// # of open issues
if (item.IsClosed == false) {
if (moment(item.CreatedDate).isAfter(twentyFourHoursAgo)) {
var openIssues = parsedData.length;
console.log(openIssues);
$('.openIssueCount').html(openIssues);
}
}
});
}
},
error: function(xhr, status) {
console.log(xhr.responseText);
console.log(xhr.status);
}
});
}
最佳答案
这里有一些示例代码来展示我在评论中所说的内容。我并不声称这是正确的——它准确地显示了我在上面尝试(但失败)用英语描述的内容。
var hiImAcounter;
hiImAcounter = 0;
$.each(parsedData, function(index, item) {
// # of open issues
if (item.IsClosed == false) {
if (moment(item.CreatedDate).isAfter(twentyFourHoursAgo)) {
hiImAcounter++;
console.log(openIssues);
$('.openIssueCount').html(openIssues);
}
}
});
// hiImAcounter now has a count
remember .each is asynchronous so if you check the counter right away it might not be set -- you would have to use a promise to guarantee the loop had finished.
关于javascript - 当满足某些条件时,如何获取数组的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58884020/