gpt4 book ai didi

jQuery:如何检查数组中是否存在函数?

转载 作者:行者123 更新时间:2023-12-01 06:30:52 24 4
gpt4 key购买 nike

我有以下代码,需要检查值是否作为数组键存在。

我似乎无法生成 key 的索引,即使它确实存在,任何帮助都会很棒。

代码:

var run = { // store the actions to trigger

block : function() {
console.log('Blocking…');
},

warning : function() {
console.log('Warning…');
}

};

console.log( $.inArray( 'warning' , run ) );

据我所知,warning 存在于 run{} 内部,并且应该返回 index1 >.

为什么上面的内容找不到(index 返回为 -1)。

感谢您的阅读。

最佳答案

run 不是一个数组(它只是一个普通对象),因此它没有索引。尽管 block 出现在 warning 之前,但对象是无序的,因此您不能说 run 的索引为 1。

要检查对象是否具有特定键,只需检查:

if ('warning' in run) {...}

或者:

if (run.warning) {...}

关于jQuery:如何检查数组中是否存在函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5055976/

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