Skip to content

Instantly share code, notes, and snippets.

@jsgoyette
Last active September 9, 2016 05:02
Show Gist options
  • Save jsgoyette/0ca51494c5202046dd1302745dcdd165 to your computer and use it in GitHub Desktop.
Save jsgoyette/0ca51494c5202046dd1302745dcdd165 to your computer and use it in GitHub Desktop.
Example vlookup with node.js
const fs = require('fs')
const _ = require('underscore');
const baby = require('babyparse');
const jmespath = require('jmespath');
// csv parse options
const parseOptions = {
header: true,
skipEmptyLines: true
};
// read files
const file1 = fs.readFileSync('file1.csv', 'utf8');
const file2 = fs.readFileSync('file2.csv', 'utf8');
// parse csv files
const parsedFile1 = baby.parse(file1, parseOptions);
const parsedFile2 = baby.parse(file2, parseOptions);
// actual logic
// add person id to order using email as lookup
_.each(parsedFile2.data, obj => {
var idByEmail = `data[?email=='${obj.email}'].id | [0]`;
obj.personId = jmespath.search(parsedFile1, idByEmail);
});
// write to file
const csvString = baby.unparse(parsedFile2.data);
fs.writeFile('output.csv', csvString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment