0
我在函數內有以下功能,我試圖檢查一個對象的狀態。問題是,當我運行代碼時,它告訴我,this.state不是一個對象。this.state不是一個對象時,在一個循環內
convertNeighbourArrayIntoMap(neighbourData) {
var neighbourCategoryMap = {}; // Create the blank map
neighbourData.forEach(function(neighbourItem) {
if(this.state.searchString == ""){
console.log("No Search String");
}
if (!neighbourCategoryMap[neighbourItem.category]) {
// Create an entry in the map for the category if it hasn't yet been created
neighbourCategoryMap[neighbourItem.category] = [];
}
neighbourCategoryMap[neighbourItem.category].push(neighbourItem);
});
return neighbourCategoryMap;
}
這工作,但我需要它在循環內。
convertNeighbourArrayIntoMap(neighbourData) {
var neighbourCategoryMap = {}; // Create the blank map
if(this.state.searchString == ""){
console.log("No Search String");
}
neighbourData.forEach(function(neighbourItem) {
if (!neighbourCategoryMap[neighbourItem.category]) {
// Create an entry in the map for the category if it hasn't yet been created
neighbourCategoryMap[neighbourItem.category] = [];
}
neighbourCategoryMap[neighbourItem.category].push(neighbourItem);
});
return neighbourCategoryMap;
}