最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript Array.shift not working - Stack Overflow

programmeradmin2浏览0评论

I have been programming a system that is supposed to delete the first index of an array. Instead of changing an array from (i.e) "1,2,3,4,5" to "2,3,4,5" the console gives an error: "Uncaught TypeError: num.splice is not a function". I have heard that num.splice is not a function, it is an operation (or something) to delete the first indexed value of the array. I am confused that when I use an example code from w3Schools, there is no outputted error in the console. I don't understand why this happens.

(I have given the entire code just in case it has to do with syntax issues)

function dCrypt() {
    var num = document.getElementById("demoin").value; // ex: a127
    var key = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
    var mod = 0;
    var out = 0;
    var prep = 0;
    var pre = 0;
    num.split("");
    mod = num[0];
    pre = key.indexOf(mod);
    num.splice(0,1);
    for (i=0;i <= pre;i++) {
        prep += 26;
    }
    out = Math.floor(num + pre);
    document.getElementById("demoout").innerHTML = out;
}

Thanks in advance!

I have been programming a system that is supposed to delete the first index of an array. Instead of changing an array from (i.e) "1,2,3,4,5" to "2,3,4,5" the console gives an error: "Uncaught TypeError: num.splice is not a function". I have heard that num.splice is not a function, it is an operation (or something) to delete the first indexed value of the array. I am confused that when I use an example code from w3Schools, there is no outputted error in the console. I don't understand why this happens.

(I have given the entire code just in case it has to do with syntax issues)

function dCrypt() {
    var num = document.getElementById("demoin").value; // ex: a127
    var key = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
    var mod = 0;
    var out = 0;
    var prep = 0;
    var pre = 0;
    num.split("");
    mod = num[0];
    pre = key.indexOf(mod);
    num.splice(0,1);
    for (i=0;i <= pre;i++) {
        prep += 26;
    }
    out = Math.floor(num + pre);
    document.getElementById("demoout").innerHTML = out;
}

Thanks in advance!

Share Improve this question edited Feb 22, 2017 at 0:47 ibrahim mahrir 31.7k5 gold badges49 silver badges78 bronze badges asked Feb 22, 2017 at 0:36 Vandesm14Vandesm14 31 silver badge5 bronze badges 1
  • 1 num is not an array. It's a string. If you want to call splice, you first have to convert it to an array. You can do this with Array.from(num). – adrice727 Commented Feb 22, 2017 at 0:42
Add a ment  | 

2 Answers 2

Reset to default 6

When you split 'num' you have to reassign it

num = num.split("");

Referring to your link from w3schools:

The splice() method adds/removes items to/from an array, and returns the removed item(s).

As you can see the var num is string(and not an array) and has the value of the element with id demoin.

Since you are trying to splice a string and not an array. The error shows up in the console.

Solution:

Either store the value of your split in an array(it could be num itself) and then splice that array.

num = num.split("");
...
num.splice(0,1);
发布评论

评论列表(0)

  1. 暂无评论