const getTitleById = (idArr) => {
for (let subject of subjectList) {
let text = '';
if (subject.id === idArr[0]) {
text = text + subject.title + ' / ';
for (let subjectChild of subject.children) {
if (subjectChild.id === idArr[1]) {
text = text + subjectChild.title;
return text;
}
}
}
}
}
直接调用上述箭头函数,为啥返回值是undefined? 用下面的普通函数写法就可以
function getTitleById(idArr) {
for (let subject of subjectList) {
let text = '';
if (subject.id === idArr[0]) {
text = text + subject.title + ' / ';
for (let subjectChild of subject.children) {
if (subjectChild.id === idArr[1]) {
text = text + subjectChild.title;
return text;
}
}
}
}
}
实测有返回啊。没返回的情况就是 for 或者 if 没进去