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

javascript - Why is this array undefined when I call .length? - Stack Overflow

programmeradmin5浏览0评论

I am working on an exercise from the book Eloquent JavaScript (see 'a list' at bottom of linked page). Basically, I want to make an object that takes an array and turns it into a list with the following structure:

var list = {
  value: 1,
  rest: {
    value: 2,
    rest: {
      value: 3,
      rest: null
    }
  }
};

When I run the code below in the console, I get TypeError: Cannot read property 'length' of undefined (line 3 in function arrayToList). Why is the array on which I am calling .length not defined?

function arrayToList(array){
    var list = {};
    if(!array.length){
        list.value = null;
    }
    else{
        list.value = array.shift();
        list.rest = arrayToList();
    }
    return list;
}

console.log(arrayToList([10, 20]));

Note: Stack Overflow question List data structures in JavaScript is a very similar post on the same problem. I'm not looking for a solution here so much as an explanation as to what is going haywire in the recursive call.

I am working on an exercise from the book Eloquent JavaScript (see 'a list' at bottom of linked page). Basically, I want to make an object that takes an array and turns it into a list with the following structure:

var list = {
  value: 1,
  rest: {
    value: 2,
    rest: {
      value: 3,
      rest: null
    }
  }
};

When I run the code below in the console, I get TypeError: Cannot read property 'length' of undefined (line 3 in function arrayToList). Why is the array on which I am calling .length not defined?

function arrayToList(array){
    var list = {};
    if(!array.length){
        list.value = null;
    }
    else{
        list.value = array.shift();
        list.rest = arrayToList();
    }
    return list;
}

console.log(arrayToList([10, 20]));

Note: Stack Overflow question List data structures in JavaScript is a very similar post on the same problem. I'm not looking for a solution here so much as an explanation as to what is going haywire in the recursive call.

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Oct 24, 2014 at 14:13 GoodwordGoodword 1,64520 silver badges28 bronze badges 3
  • 2 one idea: the function arrayToList() needs an array as an argument. In the recursive calls, it isn't getting that argument... – Goodword Commented Oct 24, 2014 at 14:16
  • Try tracing the function in a debugger. – 1983 Commented Oct 24, 2014 at 14:33
  • 5 Note that the error doesn't say that .length is undefined. – ThisSuitIsBlackNot Commented Oct 24, 2014 at 16:57
Add a ment  | 

4 Answers 4

Reset to default 12
list.rest = arrayToList();

Since you don't pass any parameter to arrayToList, array will have the default value undefined. That is why it is failing with

TypeError: Cannot read property 'length' of undefined

So, during the recursive call, you are supposed to pass an array to arrayToList, again.

function arrayToList(array) {
    var list = {};
    if (!array.length) {
        list.value = null;
    } else {
        list.value = array.shift();
        list.rest = arrayToList(array);   // Pass the `array` after shifting
    }
    return list;
}

console.log(arrayToList([10, 20]));
# { value: 10, rest: { value: 20, rest: { value: null } } }

On this line:

    list.rest = arrayToList();

… you recursively call the function but you don't specify an argument, so array bees undefined.

It looks like you should just pass array again (which you have modified).

    list.rest = arrayToList(array);

live demo

In the recursive arrayToList call, you're not passing anything as a parameter (!).

function arrayToList(array) {
    var list = {};
    if (!(array instanceof Array) || array.length == 0) {
       list.value = null;
    } else {
        list.value = array.shift();
        list.rest = arrayToList();
    }
    return list;
}
发布评论

评论列表(0)

  1. 暂无评论