Check if the elements in array have been connected into a pair in Javascript -
i have problem can't seem solve, maybe can help.
there array have. e.g. ['@dog','@cat','@mouse']
i want reiterate through each value in array , connect other values in same array (through building db query).
however, because i'll writing in database need avoid duplicates.
so if @cat
has been connected @mouse
time for
statement reaches @mouse
want skip adding connection @cat
(and @dog
because connected on first iteration @mouse
.
i've been trying for
loops, such
for (var i=0; i<animals.length; i++) { (var j = 0; j<animals.length; j++) { if (animals[i] !== animals[j]) { // adds connection between animals[i] , animals[j] } } }
but what's best way implement check of existing pairs? (where doesn't matter element first, second - e.g. graph not unidirectional).
this becomes problem if i'm going have more 4 elements in array...
thank help!
for (var i=0; i<animals.length; i++) { (var j = 0; j<i; j++) { if (animals[i] !== animals[j]) { // adds connection between animals[i] , animals[j] } } }
this way, each element of array compared against before in array. think closer want.
Comments
Post a Comment