gpt4 book ai didi

javascript - 如何在 JavaScript 中记录超出范围的数组索引?

转载 作者:可可西里 更新时间:2023-11-01 02:57:37 24 4
gpt4 key购买 nike

我有数组 var john = ['asas','gggg','ggg'];

如果我在索引 3 处访问 john,即。 john[3],它失败了。

我如何显示一条消息或警告,指出该索引没有值?

最佳答案

function checkIndex(arrayVal, index){
if(arrayVal[index] == undefined){
alert('index '+index+' is undefined!');
return false;
}
return true;
}

//use it like so:
if(checkIndex(john, 3)) {/*index exists and do something with it*/}
else {/*index DOES NOT EXIST*/}

关于javascript - 如何在 JavaScript 中记录超出范围的数组索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6521292/

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