0

我已经为此工作了 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);

当前选择列表 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);
}

未捕获的类型错误:baseChoiceList.splice 不是函数

4

1 回答 1

0

sessionStorage(and 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(',');

Stringssplit方法接受分隔符,将字符串拆分为字符串数组。

于 2015-06-12T02:43:47.813 回答