我有一個函式回傳true,或者false但我看到的是這個函式總是回傳false。
const isLastImageAttachment = index => {
const isLastImage =
filteredImages.uuid === attachments[index].uuid;
console.log(`isLastImage:`, isLastImage); // true
return isLastImage ? true : false;
};
console.log('isLastImageAttachment()', isLastImageAttachment()); // false
isLastImage回傳trueorfalse但即使is 也isLastImageAttachment總是回傳。falseisLastImagetrue
它有什么問題?
嘗試
我isLastImage自己回傳。
const isLastImageAttachment = index => {
const isLastImage =
filteredImages.uuid === attachments[index].uuid;
console.log(`isLastImage:`, isLastImage); // true
return isLastImage;
};
但isLastImageAttachment回報false。
當然只是回來true,我得到了回報true
const isLastImageAttachment = index => {
return true;
};
uj5u.com熱心網友回復:
你能在你的函式中只回傳 isLastImage 本身嗎?像這樣:
const isLastImageAttachment = index => {
const isLastImage =
filteredImages.uuid === attachments[index].uuid;
console.log(`isLastImage:`, isLastImage); // true
return isLastImage;
};
uj5u.com熱心網友回復:
您可以將其簡化為:
const isLastImageAttachment = index => filteredImages.uuid === atachments[index].uuid
為了解決您的問題,讓我們嘗試一些除錯。首先嘗試將console.log 放入您的代碼中以確保filteredImages.uuid 和attachments[index].uuid 存在。然后嘗試在 typeof 的幫助下檢查它們的型別。
也只是一個猜測,但過濾影像聽起來像一個陣列
uj5u.com熱心網友回復:
如果您 100% 確定 is "islastimage" 回傳 true,則您的代碼是正確的,只需更改我提到的內容即可
const isLastImage = filteredImages.uuid === attachments[index].uuid;
const isLastImageAttachment = index => {
const isLastImage =
filteredImages.uuid === attachments[index].uuid;
console.log(`isLastImage:`, isLastImage); // true
// instade of this
// return isLastImage ? true : false;
// just do this (I just simplified your return code line)
return isLastImage
};
console.log('isLastImageAttachment ==>', isLastImageAttachment());
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496682.html
標籤:javascript 反应
