-
-
Save masahitojp/43dcae30fe3ca0792b732b51ac866a7b 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
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