Created
September 23, 2019 11:57
-
-
Save IvanofSA/586d584617463d8b8fee00346e222825 to your computer and use it in GitHub Desktop.
reduce
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 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