Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Common Values While Looping Over Two Arrays

I have a situation where I need to compare and find common values over two arrays. I am clear on how to do it with one, but not sure how to do it in this case.

My first array looks like this:

[ { kind: 'E',
    path: [ 'short_name' ],
    lhs: 'testing',
    rhs: 'testing1' },
  { kind: 'E',
    path: [ 'agent_name' ],
    lhs: 'testing',
    rhs: 'testing2' } ]

The array above represents information pertaining to what changed on a document.

My second array looks like this:

[ { lhs: 'legacyId', rhs: 'id_number' },
  { lhs: 'name.short', rhs: 'short_name' },
  { lhs: 'name.long', rhs: 'agent_name' },
  { lhs: 'gender', rhs: 'gender' },
  { lhs: 'dob', rhs: 'date_of_birth' } ]

What I need to do is loop through and find common values for "path" in the first array's elements, and the second array's "rhs" value.

So according to my examples here, I should end up with these values being found: short_name and agent_name.

How could I write a loop to do this over the two arrays?

like image 374
Rey Avatar asked Dec 18 '22 16:12

Rey


1 Answers

You can reduce the 1st array and use a forEach loop over the second array to see if each of the values are equal, then push the value to the accumulator:

const arr1 = [{ kind: 'E', path: [ 'short_name' ], lhs: 'testing', rhs: 'testing1' }, { kind: 'E', path: [ 'agent_name' ], lhs: 'testing', rhs: 'testing2' }]
const arr2 = [{ lhs: 'legacyId', rhs: 'id_number' }, { lhs: 'name.short', rhs: 'short_name' }, { lhs: 'name.long', rhs: 'agent_name' }, { lhs: 'gender', rhs: 'gender' }, { lhs: 'dob', rhs: 'date_of_birth' }]

const common = arr1.reduce((a, o1) => {
  const match = arr2.find(o2 => o1.path[0] === o2.rhs)
  match && a.push(match.rhs)
  return a
}, [])

console.log(common)

If you truly wanted to, you could write this in one line with a find instead of a second reduce:

const a = [{ kind: 'E', path: [ 'short_name' ], lhs: 'testing', rhs: 'testing1' }, { kind: 'E', path: [ 'agent_name' ], lhs: 'testing', rhs: 'testing2' }]
const b = [{ lhs: 'legacyId', rhs: 'id_number' }, { lhs: 'name.short', rhs: 'short_name' }, { lhs: 'name.long', rhs: 'agent_name' }, { lhs: 'gender', rhs: 'gender' }, { lhs: 'dob', rhs: 'date_of_birth' }]

const common = a.reduce((a, o1) => (a.push(b.find(o2 => o1.path[0] === o2.rhs).rhs), a), [])

console.log(common)

Or, for a more performant solution ;) You could use a set:

const a = [{ kind: 'E', path: [ 'short_name' ], lhs: 'testing', rhs: 'testing1' }, { kind: 'E', path: [ 'agent_name' ], lhs: 'testing', rhs: 'testing2' }]
const b = [{ lhs: 'legacyId', rhs: 'id_number' }, { lhs: 'name.short', rhs: 'short_name' }, { lhs: 'name.long', rhs: 'agent_name' }, { lhs: 'gender', rhs: 'gender' }, { lhs: 'dob', rhs: 'date_of_birth' }]
var commonValues = []
var set = new Set([])

for (let i = 0; i < a.length; i++) { 
  const value = a[i].path[0]
  if (!set.has(value)) set.add(value)
}
for (let i = 0; i < b.length; i++) {
  const val = b[i].rhs
  if (set.has(val)) commonValues.push(val)
}

console.log(commonValues)
like image 94
Kobe Avatar answered Jan 02 '23 10:01

Kobe