我有以下两个简单的对象:
clinics: [
{
id: 1,
name: 'New Hampshire Veterinarian Clinic',
plans: [
'handle123',
'handle567',
]
},
{
id: 2,
name: 'Westminster Moltchester Clinic',
plans: [
'handle123',
'handle789',
]
}
],
animals: [
{
id: 1,
handle: 'handle123',
name: 'Cat',
},
{
id: 2,
handle: 'handle567',
name: 'Dog',
},
{
id: 3,
handle: 'haneld789',
name: 'Horse'
}
],
我有以下方法:
updateAnimals(selectedOption, id) {
}
其中 selectedOption
是 clinics
数组中的一个对象。
我想过滤第二个数组,使其只包含所选选项中提到的句柄,但我在处理参数时遇到了问题。我想实现这样的目标:
updateAnimals(selectedOption, id) {
let filteredAnimals = this.animals.filter(function({id, handle, name}) {
// Access the selectedOption here so I can use it to filter
});
}
但我不确定如何访问函数内的选定选项... 或者有更好的方法吗?
答案 0 :(得分:3)
您可以像使用范围内的任何其他变量一样使用 selectedOption,只需直接使用它selectedOption.something
:
updateAnimals(selectedOption, id) {
let filteredAnimals = this.animals.filter({ handle } => {
return selectedOption.plans.includes(handle)
});
}
答案 1 :(得分:0)
updateAnimals(selectedOption, id) {
const handles=selectedOption.plans;
let result= animals.filter(animal=>handles.includes(animal.handle));
}