const list = {"value":10,"rest":{"value":20,"rest":null}};
function list_to_array(list) {
arr = [];
for (const property in list) {
if (property == "value") {
arr.push(property);
} else if (property == "rest") {
for (const property in rest) {
arr.push(property);
}
}
}
}
console.log(JSON.stringify(list_to_array(x)));
but I get the error Uncaught ReferenceError ReferenceError: rest is not defined how can I iterate in the rest object ? or how would you do this function
this functions transform a list to an array
2
Answers
This line
x = array_to_list([10, 20]);
could bex = list_to_array([10, 20]);
You need to use a recursion here, if you the
property
is "rest". In addition to get the value uselist[property]
:However, you can shorten this recursion easily:
A simpler solution would be to use a
while
loop, and take therest
property value on each iteration: