Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make a left outer join in javascript

From an array of objects in JavaScript (D3) I need to remove every object for which a certain attribute equals another certain attribute in another array of objects, i.e. do an anti-join, i.e. a select of left columns of left join rows with introduced nulls.

anti-join

I do it with two loops but it's quite slow.

for (var i = 0; i < data1.length; i++) {
  for (var j = 0; j < data2.length; j++) {
    if (data2[j].attr3 == data1[i].attr4) {
      data2.splice(j,1);
    }
  }
}

data1.length is ~2k and data2.length is ~10k.

What is a faster method with JavaScript or jQuery or D3?

This has approximately been asked at Joins in Javascript but the solutions use external libraries.

like image 834
stallingOne Avatar asked Jan 29 '26 03:01

stallingOne


2 Answers

You need a fast lookup for the values that exist in data1, so make a map using an object:

var map = {};
for (var i = 0; i < data1.length; i++) {
  map[data1[i].attr4] = 1;
}

Then you can loop throught the items in data2 and filter them:

var result = [];
for (i = 0; i < data2.length; i++) {
  if (!(data2[i].attr3 in map)) {
    result.push(data2[i]);
  }
}
like image 57
Guffa Avatar answered Jan 30 '26 15:01

Guffa


Maybe not faster but more readable

const left = ['1', '2', '7']
const right = ['1', '3', '5', '9']

const result = left.filter((x) => !right.includes(x))
console.log(result)
like image 37
Yoruba Avatar answered Jan 30 '26 17:01

Yoruba