Created
October 15, 2019 11:41
-
-
Save IvanofSA/41b991e4a6505f65ecdc167c7b30d1cc to your computer and use it in GitHub Desktop.
MyReduce.js
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 myReduce = (obj, type, num) => { | |
let {children} = obj; | |
let newNum = check(obj, type, num); | |
if(!children) { | |
return newNum; | |
} | |
return children.reduce((acc, cur) => { | |
return myReduce(cur, type, acc) | |
}, newNum) | |
}; | |
console.log(myReduce(tree, 'file', 0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment