Javascript push for loop


#1

Hello,
I am trying to find a match and each time a match is found I want it to be pushed to the already exsisting array.

var matchSource=edgesArray.find(edges => edges.source === nodeId); // returns edge

	                         		var findTargetNode=nodesArray.find(nodes=>nodes.id===matchSource.target);
		                        	//console.log(findTargetNode);


		                        		data.edges.push(matchSource);
		                        		data.nodes.push(findTargetNode);


		                        
		                        		for (var i = 0; i < edgesArray.length; i++) {
		                        			data.edges.push(matchSource[i]);
		                        		};
		                        		console.log(data);

#2

Well, you’ll want to check whether the objects are undefined before pushing them into the arrays.

Aside from that… what part of what you’ve provided doesn’t work?

I’m not at all familiar with the sigma graph objects, which this seems to be a continuation of, but that for loop looks pretty suspicious to me as well.

@m_hutley take over!


#3

So let’s do a recap, so that I’m not the only person able to help. :wink: There’s a lot of undefined variables here that people need to know about.

How are we defining edgesArray, nodesArray, edges, and data?

Also, are you trying to find a singular edge, or multiple edges? Cause you’ve got code that looks like it’s expecting both - the first bit for a singular edge, the for loop for multiple.