gpt4 book ai didi

javascript - JavaScript 数组的 "in operator"和 "includes()"有什么区别

转载 作者:行者123 更新时间:2023-11-30 11:24:35 33 4
gpt4 key购买 nike

我的意思是,includes 是一个数组原型(prototype),但是 in 也可以与数组一起使用,那么两者之间的主要区别是什么?

最佳答案

Array.includes()检查数组中是否存在值,而 in operator检查对象中是否存在键(或数组中的索引)。

例子:

var arr = [5];

console.log('Is 5 is an item in the array: ', arr.includes(5)); // true
console.log('do we have the key 5: ', 5 in arr); // false
console.log('we have the key 0: ', 0 in arr); // true

关于javascript - JavaScript 数组的 "in operator"和 "includes()"有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48499181/

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