Click here for console output!
我正在尝试访问新过滤的数组的属性“过敏”,但会引发错误“无法读取未定义的属性”。这是我的代码:
let activePatients = patients.filter(patient => patient.mealPlans.length > 0);
console.log(activePatients); //this appears in the console!
console.log(activePatients[0]); //this too!
console.log(activePatients[0].allergies); //this throws the following error
TypeError:无法读取未定义的属性“过敏”
但是,当我写'typeOf(activePatients[0])'
时,它会记录对象。
这是患者数组的外观:
0: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
1: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
2: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
3: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
4: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(0), measurements: Array(0), …}
length: 5
__proto__: Array(0)
和activePatients数组:
0: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
1: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
2: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
3: {favoriteFoods: Array(0), dislikedFoods: Array(0), allergies: Array(0), mealPlans: Array(1), measurements: Array(0), …}
length: 4
__proto__: Array(0)
这里可能是什么问题?非常感谢!