gpt4 book ai didi

javascript - 在for循环中赋值

转载 作者:行者123 更新时间:2023-11-30 07:37:00 25 4
gpt4 key购买 nike

我正在尝试从循环中获取实际月份。当我在开发人员工具中单步执行它并且 month[i] == 4 它不会将 actualMonth 分配给 checkMonth

我是否必须将 getMonth 分配给 month[] 然后尝试查询该值?

var showCurrentMonth = function() {
var getMonth = new Date().getMonth();

var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

var actualMonth = "";
for (var i = 0; i < month.length; i++) {
var checkMonth = month[i];
console.log(month[i]);
if (getMonth == month[i]) {
actualMonth = checkMonth;
}
}
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);

最佳答案

太简单了?

var showCurrentMonth = function() {
var getMonth = new Date().getMonth();

var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];

var actualMonth = month[getMonth];

console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);

关于javascript - 在for循环中赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30120687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com