Skip to content

Instantly share code, notes, and snippets.

@masahitojp
Created August 9, 2016 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masahitojp/43dcae30fe3ca0792b732b51ac866a7b to your computer and use it in GitHub Desktop.
Save masahitojp/43dcae30fe3ca0792b732b51ac866a7b to your computer and use it in GitHub Desktop.
const targetData = [0,[1,[2,[3,[4,5,5],[4,5,5]],3],[2,[3,[4,5,5],[4,5,5]],[3,4,[4,5,5]]]],[1,[2,[3,[4,5,5],[4,5,5]],3],[2,[3,[4,5,5],[4,5,5]],3]]];
function setKey(o, key) {
const obj = Object.assign({}, o)
const str = JSON.stringify(key);
if (str in obj) {
obj[str] += 1;
} else {
obj[str] = 1;
}
return obj;
}
function f (data) {
var inner = function(data, result) {
var r = setKey(result, data);
data
.filter(function(e, i, a) {
return Array.isArray(e)
})
.forEach(function(c, i,a){
r = inner(c,r);
});
return r;
}
return inner(data, {});
}
console.info(f(targetData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment