Skip to content

Instantly share code, notes, and snippets.

@tamtamchik
Last active September 7, 2015 13:46
Show Gist options
  • Save tamtamchik/40a9f0ae7f5f50438fba to your computer and use it in GitHub Desktop.
Save tamtamchik/40a9f0ae7f5f50438fba to your computer and use it in GitHub Desktop.
compile_csv_search - my implementation, at least I didn't find such solution anywhere...
// For those who search
var compile_csv_search = function(string, column) {
var lines = string.split('\n').map(function(l){ return l.split(','); }), hash = {};
for (var i = 1, j = lines.length - 1; i <= j; i++) {
var result = {}; for(var j = 0; j < lines[0].length; j++) { result[lines[0][j]] = lines[i][j]; }
hash[lines[i][lines[0].indexOf(column)]] = result;
};
return function(name) { return hash[name]; }
}
var csv_by_name = compile_csv_search(
"ip,name,desc\n"+
"10.49.1.4,server1,Main Server\n"+
"10.52.5.1,server2,Backup Server\n",
"name");
console.log(csv_by_name("server2"));
console.log(csv_by_name("server9"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment