How to find matching column/row elements in two 2d array | JavaScript -


i want find matching column elements between 2 2d arrays

means want find matching string values in given arrays arrays :

//chars : "stack" var x = [["s",0],["t",2],["a",3],["c",1],["k",2]];  // chars: "exchange" var x = [["e",0],["x",2],["c",3],["h",1],["a",2],["n",3],["g",2],["e",3]]; 

here character "a" matching in columns, want store index/value in variable

help, how can in javascript (not in jquery)

by naive brute-force loops, should able find matches between 2 blocks this:

var x = [["s",0],["t",2],["a",3],["c",1],["k",2]]; var y = [["e",0],["x",2],["c",3],["h",1],["a",2],["n",3],["g",2],["e",3]];   function intersection(ax,bx){     var matches = [];     ax.foreach(function (a,i){         bx.foreach(function (b,j){             if (a[0]===b[0]){ // note: make sure use strict equal                 matches.push([a[0],[i,j],[a[1],b[1]]]);             }         });     }); } 

when calling intersection(x,y) should give array of intersection looks this:

[['a',[2,4],[3,2]]] 

the 2nd element indices of matching elements [2,4] 3rd element values [3,2]

if multiple matches found, you'll of matches follows:

[['a',[2,4],[3,2]], [['b',[3,5],[3,2]]]] // example 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -