I'm reading Eloquent Javascript by Marijn Haverbeke and I'm working on chapter 4's obj to array, and array to obj exercise. The solution to the array to obj is as follow:
var list = {value: 1, rest: { value: 2, rest: { value: 3, rest: null}}};
function listToArray(list){
var array = [];
for(var node = list; node; node = node.rest){
array.push(node.value);
}
return array;
}
console.log(listToArray(list));
I'm confused on what is happening on the for loop. This is what I do understand:
- variable node is equal to the list object
- the length is equal to the node which is the same as the list object
- and node is equal to the value rest
Can someone break down what is happening in the this for loop in simple terms?