how to stop array generating continously

I have 3 cells. When the combined_nodes function run once, the array keeps generating and appending to the nodes variable. I just only need to generate it once every time I execute the function. Why does this keep happening.

function combined_nodes(source, target) {
  let unique_nodes = [...new Set([...source, ...target])].map(node => ({ id: node }));
  mutable nodes = [...mutable nodes, ...unique_nodes]
  console.log(unique_nodes)
  console.log(nodes)
  console.log("generated nodes from source and target")
}

mutable nodes = []

combined_nodes(distinctSource, distinctTarget)

You are referencing both mutable nodes and nodes. When the cell updates mutable nodes, the nodes variable gets invalidated and all cells that reference it reexecute, including this cell.

1 Like