data={
notices:[
{a:1,
b:2},
{a:0,b:1}
]
}
for(x in data.notices){
x.a=2
}
console.log(data.notices);
var check=data.propertyIsEnumerable(data.notices);
alert(check);
请问为什么我想用for in函数处理JSON对象的时候,得到的结果是这个对象不能够用for in函数进行处理呢?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
for in 循环,in 前面的只是一个 key,用在数组当中就是数组下标(0, 1, 2, ...),而不是数组元素,访问数组元素还需要用属性访问运算符
data.notices[x]来进行。data.notices是数组,所以for-in的时候x不是数组的元素,而是数组的下标,所以你的x.a = 2是没有意义的:用for in当然可以,只要你喜欢,但是从效率上来说,对于明确的数组还是使用for(let i=0; i < array.length; i++){}的形式更好