Liquid Nitrogen
Liquid Nitrogen

Reputation: 3

JavaScript 2D arrays and returning matching rows

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 ] ]

Upvotes: -3

Views: 57

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386766

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; }

Upvotes: 0

Konrad
Konrad

Reputation: 24681

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)

Upvotes: 0

Aman Thakur
Aman Thakur

Reputation: 26

//You can try this :
const resultArr = array1.flatMap(x => array2
.filter(y=>y[0] === x[2])
.map(y => [...y,...x])
);

Upvotes: 1

Related Questions