Created
August 21, 2014 14:17
-
-
Save warpr/2f4184f2f75da63158a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = require('underscore'); | |
var commonPrefix = function(l) { | |
return _(l).reduce(function (memo, item) { | |
if (!memo) { | |
return item; | |
} | |
var prefix = []; | |
for (var i = 0; i < item.length; i++) { | |
if (memo[i] == item[i]) { | |
prefix.push(item[i]); | |
} else { | |
return (prefix.length < memo.length) ? prefix : memo; | |
} | |
} | |
return prefix; | |
}); | |
} | |
var data = [['a', 'b', 'c'], ['a', 'b', 'd']]; | |
var knippen = commonPrefix(data).length; | |
var result = _(data).map(function (item) { | |
return item.slice(knippen); | |
}); | |
console.log(result); |
daniel5gh
commented
Aug 21, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment