gpt4 book ai didi

JavaScript 数组缩减

转载 作者:行者123 更新时间:2023-11-28 11:17:51 25 4
gpt4 key购买 nike

function init(){
var panel = document.getElementById("panel");
var i;

var week = ["monday","tuesday","wednesday","thursday","friday"];
var weekend = ["saturday","sunday"];

panel.innerHTML+="weekdays: "+ week;
panel.innerHTML+="<hr>weekend days: "+ weekend;

for(i=0; i<weekend.length; i++)
{
week[week.length]=weekend[i];
}

panel.innerHTML+="<hr>increased with weekend days: "+week;

week-=2;

panel.innerHTML+="<hr>reduced back to weekdays: "+week;
}
window.onload=init;

这是从另一个 html 文件加载的主要 js 代码。直到最后一切都很顺利。最后的输出应该是“减少到工作日:星期一,星期二,星期三,星期四,星期五”,但我得到的是“减少到工作日:NaN”,这个错误意味着什么以及如何修复它? p>

最佳答案

您应该将数组长度减少 2,而不是数组本身:

week.length -= 2;

示例:

var week = [1,2,3,4,5,6,7];
week.length -= 2;

console.log(week);
//[1, 2, 3, 4, 5]

关于JavaScript 数组缩减,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11341371/

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