1

I'm trying to make a list from arrays in JavaScript. I originally used dot notation to define the new values of each variable. However this produces what seems like an infinite list of the first value in the array.

{value: 10, rest: {value: 10, rest:{etc...}}}

The code given as a hint is the one commented out in the function below. When run, this gives the correct output (except assigning null to the last rest value which I haven't coded in yet).

function arrayToList(array) {
  var object = {};
  for (var i = array.length - 1; i >= 0; i--) {
    object.rest = object;
    object.value = array[i];
    //object = {value: array[i], rest: object};
  }
  return object;
}

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

What is the difference between using the two different methods?

1
  • "What is the difference between using the two different methods?" Which two methods? Commented Jul 16, 2016 at 17:27

2 Answers 2

3

You are making a circular reference with object, because you use the same object for assigning to object.rest.

function arrayToList(array) {
    var object = {};
    for (var i = array.length - 1; i >= 0; i--) {
        object.rest = object;
        object.value = array[i];
        //object = {value: array[i], rest: object};
    }
    return object;
}

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

The other example overwrites object with an assignment.

function arrayToList(array) {
    var object = {};
    for (var i = array.length - 1; i >= 0; i--) {
        //object.rest = object;
        //object.value = array[i];
        object = {value: array[i], rest: object};
    }
    return object;
}

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

You could use a short ES6 version with Array#reduce for returning the rest object.

function toList(arr) {
    var result = {};
    arr.reduce((o, a) => (o.value = a, o.rest = {}), result);
    return result;
}

console.log(toList([2, 3, 5, 7, 11]));

Sign up to request clarification or add additional context in comments.

5 Comments

Remove redundant result variable.
@NinaScholz I went over the basics of arrow functions. However, they give the general format of (parameters) => {statements}. I'm wondering why your statements are wrapped with () and separated with , instead of ;.
it's just to omit the return statement, needed with curly brackets. with parenthesis and the comma operator, the arrow function returns the last value, in this case {}, assigned to o.rest.
So by putting it in parenthesis, is it being treated as "one" statement that has several parts?
right. if no parenthesis, then the part o.rest = {} is treated as parameter of reduce.
1

I think this could be another implementation for you linked list. You can simply change the link direction by replacing reduceRight with reduce.

function toList(arr) {
  return arr.reduceRight(function(rest, value) {
    return { value: value, rest: rest };
  }, null);
}

console.log(toList([1, 3]));

2 Comments

you could start with null.
@NinaScholz yes that's the fun of reduce.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.