Skip to content

Instantly share code, notes, and snippets.

@IvanofSA
Created September 23, 2019 11:57
Show Gist options
  • Save IvanofSA/586d584617463d8b8fee00346e222825 to your computer and use it in GitHub Desktop.
Save IvanofSA/586d584617463d8b8fee00346e222825 to your computer and use it in GitHub Desktop.
reduce
const tree = {
name: '/',
children:
[
{
name: 'eTc', children: [
{
name: 'config.json',
meta: {},
type: 'file'
}
],
meta: {},
type: 'directory'
},
{
name: 'hOsts',
meta: {},
type: 'file'
}
],
meta: {},
type: 'directory'
}
function check(obj, type, num) {
return obj.type === type ? num + 1 : num;
}
let reduce = (obj, type, num) => {
let {children} = obj;
let newNum = check(obj, type, num);
if(!children) {
return newNum;
}
return children.reduce((acc, cur) => {
return reduce(cur, type, acc)
}, newNum)
};
console.log(reduce(tree, 'directory', 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment