Nesting arrays of objects

by Génesis García Morilla

JavaScript

// Returns an array of n elements filled with content
let get_array = (n, content) => Array(n).fill(content);

// Returns an object with a name property that has a content value
let get_object = (name, content) => Object.defineProperty({}, name, {
    value: content
  })

// Array of objects
let a = get_array(3, {});
console.log(a);

// Object with a property that has the previous array of objects
a = get_object('b', a);
console.log(a);

// 3 levels of arrays with objects (nested)
a = 
get_array(3, get_object('b', 
  get_array(6, get_object('c', 
    get_array(3, {})
  ))
));
console.log(a);