TypeError: baseChoiceList.splice is not a function
TypeError: baseChoiceList.splice is not a function
我已经为此工作了 4 个小时,阅读了许多相关的解释并尝试了其中的几个。我确定我错过了一个简单的概念并修复了这个错误。非常感谢您的帮助。
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
sessionStorage
(和 localStorage
)都只能将 key/value 对存储为字符串。
所以你的代码:
var currentChoiceList = sessionStorage.getItem("NE_CURRENT_CHOICE_LIST");
var baseChoiceList = currentChoiceList.slice();
currentChoiceList
不是数组。它是一个字符串。
baseChoiceList
又是一个字符串,它是 currentChoiceList
的副本。 (字符串有一个 slice()
方法。)
看起来你真的想这样做:
var currentChoiceList = sessionStorage.getItem("NE_CURRENT_CHOICE_LIST");
var baseChoiceList = currentChoiceList.split(',');
字符串 split
方法接受分隔符,将字符串拆分为字符串数组。
我已经为此工作了 4 个小时,阅读了许多相关的解释并尝试了其中的几个。我确定我错过了一个简单的概念并修复了这个错误。非常感谢您的帮助。
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
sessionStorage
(和 localStorage
)都只能将 key/value 对存储为字符串。
所以你的代码:
var currentChoiceList = sessionStorage.getItem("NE_CURRENT_CHOICE_LIST");
var baseChoiceList = currentChoiceList.slice();
currentChoiceList
不是数组。它是一个字符串。
baseChoiceList
又是一个字符串,它是 currentChoiceList
的副本。 (字符串有一个 slice()
方法。)
看起来你真的想这样做:
var currentChoiceList = sessionStorage.getItem("NE_CURRENT_CHOICE_LIST");
var baseChoiceList = currentChoiceList.split(',');
字符串 split
方法接受分隔符,将字符串拆分为字符串数组。