首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError: baseChoiceList.splice不是一个函数

TypeError: baseChoiceList.splice不是一个函数
EN

Stack Overflow用户
提问于 2015-06-11 00:31:20
回答 1查看 1.8K关注 0票数 0

我已经研究了4个小时,阅读了许多相关的解释,并尝试了其中的几个。我确信我遗漏了一个简单的概念并修复了这个错误。你的帮助非常感谢。

ColorChart.setupAnswerChoices();

代码语言:javascript
运行
复制
"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

代码语言:javascript
运行
复制
consol.log  ("currentChoiceList " + currentChoiceList);

currentChoiceList 12,17,1,22,27,NCN

代码语言:javascript
运行
复制
    var what = Object.prototype.toString;
    console.log("buttonChoice  " + what.call(buttonChoice));

buttonChoice对象阵列

代码语言:javascript
运行
复制
    console.log("baseChoiceList  " + what.call(baseChoiceList));

baseChoiceList对象字符串

代码语言:javascript
运行
复制
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不是函数

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-12 02:43:47

sessionStorage (和localStorage)都只能将键/值对存储为字符串。

所以你的代码:

代码语言:javascript
运行
复制
var currentChoiceList = sessionStorage.getItem("NE_CURRENT_CHOICE_LIST");
var baseChoiceList = currentChoiceList.slice();

currentChoiceList不是数组。这是一根绳子。baseChoiceList又是一个字符串,它是currentChoiceList的副本。(字符串有一个slice()方法。)

看起来你真的想这么做:

代码语言:javascript
运行
复制
var currentChoiceList = sessionStorage.getItem("NE_CURRENT_CHOICE_LIST");
var baseChoiceList = currentChoiceList.split(',');

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30769625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档