I have a function that takes as an argument a list and must return the elements of the list into an array. For example in case of the input:
{value: 1, rest: {value: 2, rest: null}}
The output should looke like:
[1, 2]
This is how I tried to solve it:
function listToArray(list){
var arr = [];
for (var node = list; node; node = node.rest) {
arr.unshift(node);
}
return arr;
}
console.log(listToArray({value: 1, rest: {value: 2, rest: null}}));
And the output I get is:
[{value: 2, rest: null}, {
value: 1
rest: {value: 2, rest: null}
}]
Does anyone know what should I change to make it work?
arr.push(node);instead ofarr.unshift(node);will change the order of the elements, but still the output format is not the wanted one.