Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript object chain

/* List structure: a nested set of objects. NOT an array.
Each object holds reference to its successor in the object chain. */

// Build a list (object chain) using an array argument 
arrayToList = (array) => {
  let list = null;
  for (let i = array.length - 1; i >= 0; i--){
    list = {value: array[i], rest: list};
  }
  return list;
};

console.log(arrayToList(["foo", "bar"]));
// → {value: "foo", rest: {value: "bar", rest: null}}
Source by eloquentjavascript.net #
 
PREVIOUS NEXT
Tagged: #javascript #object #chain
ADD COMMENT
Topic
Name
6+4 =