“in operator"和“in operator"有什么区别?和“包括()"js [英] What is the difference between "in operator" and "includes()" js

查看:29
本文介绍了“in operator"和“in operator"有什么区别?和“包括()"js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,includes 是一个数组原型,但是 in 也可以用于数组,那么两者的主要区别是什么?

I mean, includes is an Array prototype but in can be used with arrays too, so what is the main difference between both?

推荐答案

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

Array.includes() checks for the existence of a value in an array, while the in operator checks for the existence of a key in an object (or index in the case of arrays).

示例:

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

这篇关于“in operator"和“in operator"有什么区别?和“包括()"js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆