[英]Javascript returning undefined value
这是我的 nuxt js 代码中的一种方法。
this.allCampusWithDeaprtments = campusData.map(
(item, index) => {
const schoolData = this.deptInsights.filter((row) => {
return row.unit === item.unit;
}).map(row => {
// row.departmentData = row.departmentData || [];
// row.departmentData.push(...this.depts.filter((d) => {
// return d.sisSchoolId === row.sisSchoolId;
// }));
// return row;
const departmentData = this.depts.filter(
d => d.sisSchoolId === row.sisSchoolId
);
return departmentData
});
console.log('department', departmentData);
// );
item.id = `toggle-${index}`;
return {
...item,
schoolData,
// departmentData,
hasOerData: false
};
}
);
departmentData
没有返回任何值。 可能是什么问题呢? 我想要与代码中提到的情况相匹配的数据
继续同事@sebastian-simon 的推理
您的常量在 scope 中是私有的。 因此,它没有定义。 阅读这篇文章。
试试这样:
this.allCampusWithDeaprtments = campusData.map(
let departmentData = null
(item, index) => {
const schoolData = this.deptInsights.filter((row) => {
row.unit === item.unit
departmentData = this.depts.filter((d) => {
d.sisSchoolId === row.sisSchoolId;
return departmentData
})
}
);
console.log('department', departmentData);
// );
item.id = `toggle-${index}`;
return {
...item,
schoolData,
departmentData,
hasOerData: false
};
});
据我所知,可能有两种情况:-
campusData
数组变量中传递的数据,您的条件不满足。return const departmentData = this.depts.filter((d) => {...
让我知道,如果它有帮助。
我认为你的问题出在这里:
const departmentData = this.depts.filter((d) => {
d.sisSchoolId === row.sisSchoolId;
return departmentData //
})
过滤器回调需要返回 boolean。 您正在检查条件d.sisSchoolId === row.sisSchoolId;
但不返回它,而是返回departmentData
。 所以请记住这一点,您的过滤器代码不应该是:
this.allCampusWithDeaprtments = campusData.map(
(item, index) => {
let departmentData = [];
const schoolData = this.deptInsights.filter((row) => {
departmentData.push(...this.depts.filter((d) => {
return d.sisSchoolId === row.sisSchoolId;
}));
return row.unit === item.unit;
});
console.log('department', departmentData);
// );
item.id = `toggle-${index}`;
return {
...item,
schoolData,
departmentData,
hasOerData: false
};
}
);
编辑新结构
this.allCampusWithDeaprtments = campusData.map(
(item, index) => {
// let departmentData = [];
const schoolData = this.deptInsights.filter((row) => {
return row.unit === item.unit;
}).map(row => {
row.departmentData = row.departmentData || [];
row.departmentData.push(...this.depts.filter((d) => {
return d.sisSchoolId === row.sisSchoolId;
}));
return row;
});
// console.log('department', departmentData);
// );
item.id = `toggle-${index}`;
return {
...item,
schoolData,
// departmentData,
hasOerData: false
};
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.