code: function flattenList(list) {
    // Make a copy, so the original tensor is not modified.
    list = [].concat(list);
  
    // Note that i must be checked against the length of the list each time through the loop, as the
    // list is modified within the iterations.
    for (let i = 0; i < list.length; i++) {
      if (Array.isArray(list[i])) {
        // Replace the item with the flattened version of the item (using the ... operator).
        // Replace with the items and backtrack 1 position
        list.splice(i, 1, ...list[i]);
  
        // Decrement i to look at the element again; we'll keep looking at this i index, until
        // the most deeply nested item has been flattened.
        i--;
      }
    }
  
    return list;
  }
docs: Flatten the list. This is only relevant for multi-dimensional tensors.