我已經爲此工作了4個小時,並閱讀了許多相關的解釋並嘗試了其中的幾個。我確信我錯過了一個簡單的概念並修復了這個錯誤。非常感謝您的幫助。TypeError:baseChoiceList.splice不是函數
ColorChart.setupAnswerChoices();
"setupAnswerChoices": function() {
var currentChoiceList = sessionStorage.getItem("NE_CURRENT_CHOICE_LIST");
var baseChoiceList = currentChoiceList.slice();
console.log ("baseChoiceList " + baseChoiceList);
baseChoiceList 12,17,1,22,27,NCN
consol.log ("currentChoiceList " + currentChoiceList);
currentChoiceList 12,17,1,22,27,NCN
var what = Object.prototype.toString;
console.log("buttonChoice " + what.call(buttonChoice));
buttonChoice [object Array]
console.log("baseChoiceList " + what.call(baseChoiceList));
baseChoiceList [object String]
var buttonChoice = [];
for (var i = 0; i < 5; i++) {
var randomButtonIndex = Math.floor(Math.random() * (5 - i));
buttonChoice = baseChoiceList.splice(randomButtonIndex,1);
}
Uncaught TypeError: baseChoiceList.splice is not a function
Norman Breau是正確的。建議使用JSON.stringify和JSON.parse替代方法。然而,Norman使用.split的解決方案正是這種情況下所需的輕量級直接解決方案。 – Rich