function convertArrayToList(inputArray) {
let list = null;
for (let i = inputArray.length - 1; i >= 0; i--) {
list = { value: inputArray[i], rest: list };
}
return list;
}
let result = convertArrayToList([10, 20]);
console.log(JSON.stringify(result));
The output I am currently getting is:
{"value":10,"rest":{"value":20,"rest":{"rest":null}}}
But what I actually want is:
{value: 10, rest: {value: 20, rest: null}}
How can I solve this issue? Specifically, how do I change the last "rest" to null instead of another object?