I am trying to convert an array to list(containing objects) and first object is connected to next pair value.
This is what I have done so far:
var myArray = [1,2,3] ;
function arrayToList()
{
var myList = Object.keys(myArray).map(function(key){
return { value: myArray[key],rest:myArray[key]}
});
console.log(myList);
}
arrayToList(myArray);
This should return Like this:
var list = {
value: 1,
rest: {
value: 2,
rest: {
value: 3,
rest: null
}
}
};
, when given an array of [1,2,3]
rest:myArray[key], callarrayToList()again - but give an offset so you will eventually stop recursing.Object.keys(myArray)makes no sense since you have an array, not an object.null?