in
Iterate over objects
let names = {
name: 'Alice',
age:20,
address:"beijing"
};
//name是属性
for(let name in names){
console.log("属性是:"+name+"值是:"+names[name])
}
//判断某个属性是否在对象中,属性名字一定要加引号
console.log("name" in names)
console.log("Name" in names)
console.log("age" in names)
Iterate over the array
let arr = [7,5]
for (let i in arr)
{
console.log("索引:"+i+"值"+arr[i])
}
console.log(7 in arr)
console.log("7" in arr)
console.log(1 in arr)
To summarize: in the object name is the attribute in the corresponding object, and the attribute value in the array is the index. To determine whether an attribute should be enclosed in quotation marks in the object, otherwise an undefine error will occur;In the array, whether or not quotation marks are used, the default is an index, not a value! ! ! ! ! Remember
includes usage
The includes() method is used to judge whether an array contains a specified value, if it returns true, otherwise false, the first parameter is the value to be judged, the second is to judge from the index of the array, the default is 0
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(3, 3); //false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
console.log([1,2,3].includes(1,1))//false
If there are any mistakes, you are welcome to criticize and correct.