skip to Main Content

I have 2 arrays, each is like a database table row set.

array1 = [
['AB2C', 'Red', 113],
['BE4F', 'Green', 164],
['AE3G', 'Blue', 143],
]

array2 = [
[143, 'FabricB2', 'W5'],
[189, 'FabricC9', 'W4'],
[113, 'FabricA3', ' W5'],
[143, 'FabricD1', 'W6']];

I want to do a join and find matching rows, returning the matched rows in array2 along with the matched row in array1. The returned matching array should look like this:

[143, 'FabricB2', 'W5',  'AE3G', 'Blue', 143],
[113, 'FabricA3', ' W5', 'AB2C', 'Red', 113],
[143, 'FabricD1', 'W6',  'AE3G', 'Blue', 143]

I tried to use JavaScript methods map(), filter(), flatMap(), spread operator but can’t get the result.
Anyone can help me with this? With the shortest code? The one I have below does not work.

function testArrayFunction() {

array1 = [
['AB2C', 'Red', 113],
['BE4F', 'Green', 164],
['AE3G', 'Blue', 143],

];

array2 = [
[143, 'FabricB2', 'W5'],
[189, 'FabricC9', 'W4'],
[113, 'FabricA3', ' W5'],
[143, 'FabricD1', 'W6']];

var array1Element = 2;
var array2Element = 0;

var res = array1
.map(x => [  ...  array2
.filter(    y => y[array2Element] === x[array1Element ] )  ,...x ] );

console.log(res);

}

Does not give the expected result which is this

[143, 'FabricB2', 'W5', 'AE3G', 'Blue', 143],
[113, 'FabricA3', ' W5', 'AB2C', 'Red', 113],
[143, 'FabricD1', 'W6',  'AE3G', 'Blue', 143]

Gives this which is not what I want

Info    
[ [ [ 113, 'FabricA3', ' W5' ], 'AB2C', 'Red', 113 ],
[ 'BE4F', 'Green', 164 ],
[ [ 143, 'FabricB2', 'W5' ],
[ 143, 'FabricD1', 'W6' ], 'AE3G', 'Blue', 143 ] ]

3

Answers


  1. //You can try this :
    const resultArr = array1.flatMap(x => array2
    .filter(y=>y[0] === x[2])
    .map(y => [...y,...x])
    );
    
    Login or Signup to reply.
  2. It’s easier to use reduce

    const array1 = [
      ['AB2C', 'Red', 113],
      ['BE4F', 'Green', 164],
      ['AE3G', 'Blue', 143],
    ]
    
    const array2 = [
      [143, 'FabricB2', 'W5'],
      [189, 'FabricC9', 'W4'],
      [113, 'FabricA3', ' W5'],
      [143, 'FabricD1', 'W6']
    ];
    
    
    const result = array2.reduce((result, current) => {
      const other = array1.find(b => current[0] === b[2])
      if (other) {
        result.push([...current, ...other])
      }
      return result
    }, [])
    
    console.log(result)
    Login or Signup to reply.
  3. You could take a reference to the items of array1 and return items of array2 only if a reference to array1 exists.

    const
        array1 = [['AB2C', 'Red', 113], ['BE4F', 'Green', 164], ['AE3G', 'Blue', 143]],
        array2 = [[143, 'FabricB2', 'W5'], [189, 'FabricC9', 'W4'], [113, 'FabricA3', ' W5'], [143, 'FabricD1', 'W6']],
        references = Object.fromEntries(array1.map(a => [a[2], a])),
        result = array2.reduce((r, a) => {
            if (references[a[0]]) r.push([...a, ...references[a[0]]])
            return r;
        }, []);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search