我已经研究了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对象阵列
console.log("baseChoiceList " + what.call(baseChoiceList));baseChoiceList对象字符串
var buttonChoice = [];
for (var i = 0; i < 5; i++) {
var randomButtonIndex = Math.floor(Math.random() * (5 - i));
buttonChoice = baseChoiceList.splice(randomButtonIndex,1);
}Uncaught : baseChoiceList.splice不是函数
发布于 2015-06-12 02:43:47
sessionStorage (和localStorage)都只能将键/值对存储为字符串。
所以你的代码:
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方法接受分隔符,将字符串拆分为字符串数组。
https://stackoverflow.com/questions/30769625
复制相似问题