我知道作用域有问题,但不知道如何解决。我有两个功能。mapSubject
就是要获取数据并重新构建它。subjectFilter
是用重建的数据来渲染某物。那么,我该如何在subjectFilter
中设置"majorArray"作为参数呢?
const subjectFilter = (arr) => {
arr.map(sub => {
return (
<div>
<label>{sub}</label>
<input
type="checkbox"
placeholder={sub}
className="subject"
/>
</div>
)
})
}
const mapSubject = (data) => {
const majorArray = []
data.map(item => {
if (item.major1 && !majorArray.includes(item.major1)) {
majorArray.push(item.major1);
}
return majorArray
})
console.log(majorArray)
};
return(
<div>
{subjectFilter()}
</div>
)
您可能可以作为
const subjectFilter = (arr) => {
return arr.map((sub) => {
return (
<div>
<label>{sub}</label>
<input type="checkbox" placeholder={sub} className="subject" />
</div>
);
});
};
const mapSubject = (data) => {
const majorArray = [];
data.forEach((item) => {
if (item.major1 && !majorArray.includes(item.major1)) {
majorArray.push(item.major1);
}
});
return majorArray
};
return <div>{subjectFilter(mapSubject())}</div>;