Joining Data

Overview


Joining data occurs when you take two table like datasets (meaning arrays of objects), and you create a single array of objects. This is accomplished by matching records from one dataset with records from the other dataset by comparing the value of a field or fields. The resulting record will take data from each matched record.

The davinci library provides a module for joining data.

join.js

Straight Join


A straight join is a traditional join, where you match two records based on a set of fields in each record. The join library provides a join method with the following inputs:

  • left data
  • right data
  • left keys : a function that returns the values that you want to join on. Typically, you just return an array with the value of the key fields
  • right keys
  • select: function that is passed a left record and a right record and returns what the record should be in the result set.

let jn = await import('/lib/join/v1.0.0/join.js');

var data = [
	{ age: 12, name: 'dan', id:1 },
	{ age: 12, name: 'an', id:2 },
	{ age: 12, name: 'an', id:3 },
	{ age: 12, name: 'dan3', id:4 },
	{ age: 12, name: 'dan3', id:5 },
	{ age: 12, name: 'dan3', id:6 },
	{ age: 12, name: 'dan', id:7 },
	{ age: 12, name: 'an', id:8 }
];

var data2 = [
	{ age: 12, name: 'dan', key:'one' },
	{ age: 13, name: 'an', key:'two' },
	
];

//join(leftdata, rightdata, leftkeys, rightkeys, select)
let data1 = jn.join(data, p=>[p.name, p.age], data2, p=>[p.name, p.age], (left, right)=>{
    if(left != undefined && right != undefined){
        return {
            ...left,
            ...right
        }
    }
}).value();
						 
Try it

Contents