1
setup() {
    var nodes = {
      node1: { name: "Node 1" },
      node2: { name: "Node 2" },
      node3: { name: "Node 3" },
      node4: { name: "Node 4" },
    }
    var edges = {
      edge1: { source: "node1", target: "node2" },
      edge2: { source: "node2", target: "node3" },
      edge3: { source: "node3", target: "node4" },
    }
    return { nodes, edges }
  }

So instead of writing node1,node2, and so on, is there a way I can loop and create the exact same thing? Any suggestions are appreciated.

harsh panday
  • 83
  • 1
  • 10

5 Answers5

1

Yes. You can dynamically create property names using something like Object.fromEntries.

Object.fromEntries creates an object from a series of entries, where an "entry" is an array with two elements: a property name, and a value for that property.

Here's an example that recreates your data above:

function setup() {
    const nodes = Object.fromEntries(
      [1,2,3,4].map(n => ["node"+n, { name: "Node " + n }])
    )
    const edges = Object.fromEntries(
      [1,2,3].map(n => [
        "edge"+n, 
        { source: "node" + n, target: "node" + String(n+1) }
      ])
    )
    return { nodes, edges }
 }
 console.log(setup())
David P. Caldwell
  • 3,394
  • 1
  • 19
  • 32
1

Here's a solution:

const setup = (nodeCount) => {
  const nodes = {};
  const edges = {};

  for (let i = 1; i <= nodeCount; i++) {
    nodes[`node${i}`] = {
      name: `Node ${i}`
    }
    
    if (i <= nodeCount - 1) {
      edges[`edge${i}`] = {
        source: `node${i}`,
        target: `node${i + 1}`
      }
    }
  }

  return { nodes, edges };
}

console.log(setup(4));
NetSkylz
  • 414
  • 2
  • 8
  • 1
    I think you should remove the "simple solution" and only have the complete answer instead. – kelsny May 20 '22 at 19:22
  • Thank you for the answer. I tried this and It worked perfectly fine yesterday, but today when I re-ran the code it started giving me this error ```Element implicitly has an "any" type because expression of type "edge${number}" can't be used to index type "{}".``` Do you know why am I getting this? PS:The frontend is working fine, it's just showing this error. – harsh panday May 21 '22 at 16:27
  • @harshpanday It seems like something has been wrongly typed. There are multiple questions about the same problem on stackoverflow, like here: https://stackoverflow.com/questions/57086672 – NetSkylz May 21 '22 at 16:45
  • @NetSkylz yeah I looked at a lot of solutions, nothing worked for me and the code was running perfectly yesterday with the exact same code. Today suddenly it gave that error, the frontend is still doing what it needs to do, but the console is showing this error. – harsh panday May 21 '22 at 17:34
  • @harshpanday Did you tried to type your nodes and edges object with something like this: type nodes = { [key: string]: string } ? If you didn't find any answer solving your problem, you should post a new question on Stackoverflow, as your error is related to Typescript and we can't help you without knowing more about your code. – NetSkylz May 21 '22 at 17:54
  • @NetSkylz Yeah I tried that, I'll post a new question. – harsh panday May 21 '22 at 18:34
0

Sure you can! Many ways, one of the common ones is:

const nodes = Object.fromEntries(
  Array.from(Array(10).keys())
    .map(i => [`node${i + 1}`, {name: `Node ${i + 1}`}])
);

/* nodes are:
{
    "node1": {
        "name": "Node 1"
    },
    "node2": {
        "name": "Node 2"
    },
    "node3": {
        "name": "Node 3"
    },
    "node4": {
        "name": "Node 4"
    },
    "node5": {
        "name": "Node 5"
    },
    "node6": {
        "name": "Node 6"
    },
    "node7": {
        "name": "Node 7"
    },
    "node8": {
        "name": "Node 8"
    },
    "node9": {
        "name": "Node 9"
    },
    "node10": {
        "name": "Node 10"
    }
}
*/
Igor Loskutov
  • 2,157
  • 2
  • 20
  • 33
0

The following code has the same result but uses for-loops, please be more specific in your question if you need something else.

setup() {
    var nNodes = 4
    var nEdges = 3
    var nodes = {}
    var edges = {}

    for(i = 1 ; i < nNodes + 1; i++){
        nodes['node'+i.toString()] = {"name": "Node " + i.toString()}
    }
    for(i = 1 ; i <nEdges + 1; i++){
        edges['edge'+(i+1).toString()] = {"source": "node"+i.toString(), "target": "node"+(i+1).toString()}
    }
    return { nodes, edges }
  }
Guillaume
  • 166
  • 1
  • 11
0

Try this (I just added a logic to generate the objects) :

const nodes = {}
var edges = {}

for (let i = 1; i <= 4; i++) {
  nodes['node' + i] = { name: 'Node ' + i }
  if (i !== 4) {
    edges['edge' + i] = { source: 'node' + i, target: 'node' + (i + 1)
    }
  }
}

console.log(nodes);
console.log(edges);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123